I created a load function to read a file. My problem is that the data is an array that I need to decode.
def load(input_file):
f = open(input_file,"rb")
if f.mode == "rb":
contents = f.read()
print(contents)
load('/path/data/text.txt')
The results is the following that I need to decode
b'\xc3\x84\x03cjoblib.numpy_pickle\nNumpyArrayWrapper\nq\x00)\
xc3\x85q\x01}q\x02(X\x08\x00\x00\x00subclassq
\x03cnumpy\nndarray\nq\x04X\x05\x00\x00
3 Answers 3
It looks like the data is a Python pickle, you should use the pickle module for loading it
pickle.load(input_file)
Comments
It looks like your file contains pickle'd data.
Assuming you trust the file, then you can load it with pickle.load:
arr = pickle.loads(content)
However, please be aware that loading pickled content is super dangerous, so make sure you can absolutely trust the file (e.g., you know that you created it and it has not been modified) before loading it).
pickle can be useful in certain cases, such as sharing data between two separate Python processes.
Another option: since it looks like you're storing a numpy array, it would be better and easier (maybe faster) to use numpy's tofile function.
arr = numpy.arange(10)
arr.tofile('array.bin')
loaded_arr = numpy.fromfile('array.bin', numpy.int)
3 Comments
tofile and fromfile are meant to show two different things: 1) how you would save an array to file (tofile), and then how you could later load that same array back from the file (fromfile). numpy.int specifies the data type, without it, numpy assumes the values are floating-point. In your case, you'd have to know the data type of your array and use that.You're missing out on the conditional and not entering it.
contents should be under the if statement
def load(input_file):
f = open(input_file,"rb")
if f.mode == "rb":
contents = f.read()
print(contents)
load('/path/data/text.txt')