Python Forum

Full Version: TypeError: ENCODE Method, str instead of byte
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
The code line in bold is giving me the following error, please help:
Traceback (most recent call last):
File "bath_dataset_lit_link.py", line 81, in <module>
link_list.write(str(doi + '\t' + data_creators.encode("utf-8") + '\t' + lit_doi.encode("utf-8")+ '\t' + "Title: " + title.encode("utf-8") +"; Publisher: "+ pub_names.encode("utf-8") + "; Creators: "+ lit_creators.encode("utf-8") +'\n'))
TypeError: must be str, not bytes
This looks like trying to take some python2 code (where you might have needed to encode a string yourself) and run it in python3.

If python3, don't encode the string. Just pass the string to write() and it will encode it. You can pick a specific encoding at open() time, but normally you'll just use the default for your platform.