Python Forum
TypeError: ENCODE Method, str instead of byte - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: TypeError: ENCODE Method, str instead of byte (/thread-26676.html)



TypeError: ENCODE Method, str instead of byte - Rajath - May-09-2020

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


RE: TypeError: ENCODE Method, str instead of byte - bowlofred - May-09-2020

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.