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
Python: Converting from Iso-8859-1/Latin1 to Utf-8
Add X and Y Labels to a Pandas Plot
Retrieve List of Tasks in a Queue in Celery
Python Assigning Multiple Variables to Same Value? List Behavior
Python - 'Ascii' Codec Can't Decode Byte
Python Script to Copy Text to Clipboard
Writing a Dict to Txt File and Reading It Back
How to Convert CSV File to Multiline JSON
Split Text Lines in Scanned Document
How to Clone a Django Model Instance Object and Save It to the Database
How to Create Key or Append an Element to Key
When Would the -E, --Editable Option Be Useful with Pip Install
How to Dynamically Create Derived Classes from a Base Class
How to Send Cookies in a Post Request with the Python Requests Library
In Python, How to Load Yaml Mappings as Ordereddicts
How to Sum Values in a Column That Match a Given Condition Using Pandas