提问人:Enstein Sugumar 提问时间:11/23/2019 最后编辑:Ismael PadillaEnstein Sugumar 更新时间:10/27/2023 访问量:15276
TypeError:“str”对象的描述符“encode”不适用于“tuple”对象
TypeError: descriptor 'encode' for 'str' objects doesn't apply to a 'tuple' object
问:
有人可以帮助修复TypeError吗?它在 python 2 中有效,但在 python 3 中无效。
蟒蛇2:
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')
蟒蛇3:
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')
答:
0赞
Igor Tischenko
11/23/2019
#1
答案就在问题中 - 你有一个元组对象而不是字符串。
评论
0赞
Enstein Sugumar
11/23/2019
如果有办法将上述python2代码转换为python3,请告诉我。我在其他线程/站点上建议的大多数方法都出错了
0赞
Enstein Sugumar
11/26/2019
#2
它将文件打开为“w”而不是“wb”后工作
with open(RECXX_Output) as infile ,open(Modified_Recxx_Output, 'w') as outfile:
评论
outfile.write(b','.join(appendb)+b'\n')
outfile.write(','.join(appendb)+'\n')