将Dict写入CSV时出现UnicodeEncodeError

时间:2020-05-25 17:30:01

标签: python python-2.7 csv dictionary

以下错误:“ UnicodeEncodeError:'ascii'编解码器无法在位置2编码字符u'\ xc4':顺序号不在范围(128)中”

mydict = { 
          'SNo': ['1','2','3'],
          'Name': ['Dinesh', 'Rajesh', 'Mahesh'],
          'Grade': ['α', 'β', 'γ']
          }

with open('info.csv', 'wb') as f:
    header = mydict.keys()
    writer = csv.DictWriter(f, fieldnames=header)
    writer.writeheader()
    for pivoted in zip(*mydict.values()):
        writer.writerow(dict(zip(header, pivoted)))

Python 2.7版

预期产量

enter image description here

Error: UnicodeEncodeError: 'charmap' codec can't encode character '\u03b1' in position 9: character maps to <undefined>

0 个答案:

没有答案