# 输出一维csv
ls = ['北京', '上海', '天津', '重庆']
ls_str = ','.join(ls)
f = open('a.csv', 'w', encoding='utf-8')
f.write(ls_str)
f.close()
f = open('a.csv', 'r', encoding='utf-8')
r = f.read()
print(r)
f.close()
交互页面显示正常,但excel中乱码
将encoding
的值修改为'utf-8-sig'
可以解决这个问题
# 输出一维csv
ls = ['北京', '上海', '天津', '重庆']
ls_str = ','.join(ls)
f = open('a.csv', 'w', encoding='utf-8-sig')
f.write(ls_str)
f.close()
f = open('a.csv', 'r', encoding='utf-8-sig')
r = f.read()
print(r)
f.close()