如何修复将列表转换为csv格式时的编码错误?

2024-09-27 07:33:01 发布

您现在位置:Python中文网/ 问答频道 /正文

当我试图将我的unicode列表写入csv文件时,我得到了AttributeError: 'tuple' object has no attribute 'encode'"

with open('assignmentTest.csv', 'wb') as finale:
writer = csv.writer(finale) #creates csv file to write final lists into
finalRows = zip(firstName, lastName, phdName, universityName, departmentName) #put all of the lists into another lists so that the outputs are in 'column form' as opposed to rows
for rowToken in finalRows: #puts each element of each list together in the same order
    conver = rowToken
    writer.writerow(conver.encode('utf-8'))

最初(没有.encode('utf-8'))我收到错误:

^{pr2}$

有人知道如何解决这个问题,这样我就可以写我的单子了吗?在


Tags: ofcsvthetoinaslistsencode
1条回答
网友
1楼 · 发布于 2024-09-27 07:33:01

'tuple' object has no attribute 'encode'

只能对字符串进行编码(具体地说,Unicode字符串到字节字符串)。在

rowToken不是字符串,而是字符串列表。你必须单独编码里面的每个字符串。例如:

^{1}$

相关问题 更多 >

    热门问题