TypeError: descriptor 'encode' for 'str' objects doesn't apply to a 'tuple' object

Enstein Sugumar picture Enstein Sugumar · Nov 22, 2019 · Viewed 7.2k times · Source

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')

Answer

Igor  Tischenko picture Igor Tischenko · Nov 22, 2019

The answer is in the question - You have a tuple object and not string.