'TypeError: descriptor 'encode' for 'str' objects doesn't apply to a 'tuple' object
Can some please help in fixing the TypeError. It works in python 2 but not in python 3.
Python2:
def ExchangeColumns(RECXX_Output,Modified_Recxx_Output,column1,column2,column3,column4,column5,column6):
with open(RECXX_Output) as infile ,open(Modified_Recxx_Output, 'wb') as outfile:
reader = csv.DictReader(infile)
append = (column1,column2,column3,column4,column5,column6)
outfile.write(','.join(append)+'\n')
Python3:
def ExchangeColumns(RECXX_Output,Modified_Recxx_Output,column1,column2,column3,column4,column5,column6):
with open(RECXX_Output) as infile ,open(Modified_Recxx_Output, 'wb') as outfile:
reader = csv.DictReader(infile)
append = (column1,column2,column3,column4,column5,column6)
appendb = str.encode(append)
outfile.write(b','.join(appendb)+b'\n')
##outfile.write(b','.join(append).encode(encoding='utf-8')+b'\n')
Solution 1:[1]
The answer is in the question - You have a tuple object and not string.
Solution 2:[2]
It worked after opening the file as 'w' instead of 'wb'
with open(RECXX_Output) as infile ,open(Modified_Recxx_Output, 'w') as outfile:
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Igor Tischenko |
| Solution 2 | Enstein Sugumar |
