Python 类型错误:描述符';编码';对于';str';对象不';t适用于';元组';对象
请帮我修一下打字错误。它在Python2中工作,但在Python3中不工作 蟒蛇2:Python 类型错误:描述符';编码';对于';str';对象不';t适用于';元组';对象,python,python-3.x,type-conversion,ride,Python,Python 3.x,Type Conversion,Ride,请帮我修一下打字错误。它在Python2中工作,但在Python3中不工作 蟒蛇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.
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')
答案在问题中-您有一个元组对象而不是字符串。答案在问题中-您有一个元组对象而不是字符串 在以“w”而不是“wb”打开文件后,它就工作了 在以“w”而不是“wb”打开文件后,它就工作了
需要最少的可复制代码来帮助您找到解决方案那么,您为什么要将其编写为字节字符串
outfile.write(b','.join(appendb)+b'\n')
而不仅仅是outfile.write(','.join(appendb)+'\n')
?outfile.write(','.join(appendb)+'\n')-仍然会出现相同的错误-TypeError:str对象的描述符“encode”不适用于“tuple”对象需要最小的可复制代码来帮助您找到解决方案所以,为什么要把它写成字节字符串outfile.write(b','.join(appendb)+b'\n')
而不仅仅是outfile.write(','.join(appendb)+'\n')
?outfile.write(','.join(appendb)+'\n'))-仍然收到相同的错误-TypeError:“str”对象的描述符“encode”不适用于“tuple”对象请告诉我是否有方法将上述python2代码转换为python3。对于其他线程/站点上建议的大多数方法,我都遇到了错误。请告诉我是否有办法将上述python2代码转换为python3。对于其他线程/站点上建议的大多数方法,我都遇到了错误
with open(RECXX_Output) as infile ,open(Modified_Recxx_Output, 'w') as outfile: