0

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
asked Jun 26, 2019 at 17:39
0

3 Answers 3

2

It looks like the data is a Python pickle, you should use the pickle module for loading it

pickle.load(input_file)
answered Jun 27, 2019 at 2:15
Sign up to request clarification or add additional context in comments.

Comments

1

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)
answered Jun 27, 2019 at 2:19

3 Comments

Thanks Nikolas. The last line "loaded_arr = numpy.fromfile()" is not clear. Why a "tofile" at first and then a "fromfile"? Also what is numpy.init? Sorry, but I am not familiar with this.
No problem, @Annalix! 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.
Thanks a lot @Nicholas. It is a numpy array actually and it does seem to work.
1

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')
answered Jun 27, 2019 at 2:13

1 Comment

yes you are right. Don't trust the indentation of the script here, in the code is correct

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.