Python Forum
TypeError: ENCODE Method, str instead of byte
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
TypeError: ENCODE Method, str instead of byte
#1
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
Reply
#2
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.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  encode/decode to show correct country letters in a CTk combobox janeik 2 657 Sep-02-2023, 09:46 AM
Last Post: janeik
Question UnicodeEncodeError: 'ascii' codec can't encode character u'\xe8' in position 562: ord ctrldan 23 4,599 Apr-24-2023, 03:40 PM
Last Post: ctrldan
  UnicodeEncodeError: 'ascii' codec can't encode character '\xfd' in position 14: ordin Armandito 6 2,641 Apr-29-2022, 12:36 PM
Last Post: Armandito
  'NoneType' object has no attribute 'encode' bhagyashree 6 8,737 Nov-05-2020, 03:50 PM
Last Post: deanhystad
  'utf-8' codec can't decode byte 0xe2 in position 122031: invalid continuation byte tienttt 12 11,344 Sep-18-2020, 10:10 PM
Last Post: tienttt
  how to encode and decode same value absolut 2 2,279 Sep-08-2020, 09:46 AM
Last Post: TomToad
  'utf-8' codec can't decode byte 0xda in position 184: invalid continuation byte karkas 8 31,468 Feb-08-2020, 06:58 PM
Last Post: karkas
  TypeError: type str doesn't define __round__ method emmapaw24 7 29,187 Feb-03-2020, 08:38 PM
Last Post: snippsat
  asyncio encode and decode, how good are they? CoderOne 2 2,292 Sep-03-2019, 11:06 PM
Last Post: wavic
  UnicodeEncodeError: 'charmap' codec can't encode character '\u2019' in position 23: c kapilan15 2 4,375 Jan-14-2019, 09:11 PM
Last Post: Gribouillis

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020