Using Pickle.Dump - Typeerror: Must Be Str, Not Bytes

Error on saving to Pickle - TypeError: write() argument must be str, not bytes

It's hard to debug without a proper stack trace, please add one.

That said, you are opening the pickle file for writing and then opening it for reading. This is unnecessary and probably what's failing here.

This:

open('level1_classifier.pk', 'wb')
with open ('level1_classifier.pk') as l1clf:
pickle.dump(nb, l1clf)

Should probably be like this:

with open('level1_classifier.pk', 'wb') as l1clf:
pickle.dump(nb, l1clf)

See this guide for more details

Python3.4 pickle dump function fails with TypeError

You need to open in binary mode i.e wb:

 open('people_pickle', 'wb')

Lexer Outputs TypeError: write() argument must be str, not bytes in Python. What am I doing wrong?

The error message is not very helpful. After some trial and error I got it to work with

output = open("C:\\Users\Asher\Documents\BUSlang\lexer_output", "wb")

i.e. the output file you want to write to must be opened in binary mode.

Another note: I'm not sure why you are using global output, in your sample code it is not needed.

builtins.TypeError: must be str, not bytes

The outfile should be in binary mode.

outFile = open('output.xml', 'wb')

Getting TypeError must be str not bytes with echo command

You could convert the strings to bytes as well and then decode them:

bytes_string = b"echo \"" + base64.b64encode(b'Hello World') + b"\" | base64 -d"
print(bytes_string.decode('utf-8'))

>>> echo "SGVsbG8gV29ybGQ=" | base64 -d


Related Topics



Leave a reply



Submit