如何在python中将unicode列表转换为utf8格式?

2024-09-30 16:21:05 发布

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

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
        writer.writerow(rowToken)

我试图找到一种有效的方法来将列表转换成utf8格式,同时又不破坏我的writer.writerow(rowToken)-基本上就是我用来为列表创建单独列的方法。在


Tags: ofcsvtheto方法inaslists
1条回答
网友
1楼 · 发布于 2024-09-30 16:21:05

@dimitrijim现在删除的答案是正确的。您是否意外地使用了writerows而不是writerow?在

#!python2
#coding:utf8

import csv

firstName = u'John Joe 马克'.split()
lastName = u'Doe Blow 多路能'.split()
phdName = u'a b c'.split()
universityName = u'd e f'.split()
departmentName = u'g h i'.split()

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
        writer.writerow([col.encode('utf8') for col in rowToken])

以UTF-8编码的输出文件内容:

^{pr2}$

相关问题 更多 >