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

16k views Asked by At

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

There are 2 answers

1
Igor  Tischenko On

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

0
Enstein Sugumar On

It worked after opening the file as 'w' instead of 'wb'

with open(RECXX_Output) as infile ,open(Modified_Recxx_Output, 'w') as outfile: