UnicodeEncodeError,字符为u'\ u2013

问题描述 投票:0回答:1

我收到以下错误,短划线字符“ - ”

UnicodeEncodeError: 'ascii' codec can't encode character u'\u2013' in position 38: ordinal not in range(128)

我尝试使用以下:skills.encode('utf-8'),但我仍然得到错误。下面是我试图写入csv的代码。

 writer.writerow([name.encode('utf-8'),
                 heading.encode('utf-8'),
                 location.encode('utf-8'),
                 education.encode('utf-8'),
                 summary,
                 currentRole,
                 allRoles,
                 companiesFollowed,
                 groups,
                 skills.encode('utf-8')])
python ascii
1个回答
1
投票

您可以在str.encode关键字下为errors指定一些设置。更多信息可以在in the docs找到,但我建议你使用'replace'错误处理程序。

writer.writerow([name.encode('utf-8', errors='replace'),
    heading.encode('utf-8', errors='replace'),
    location.encode('utf-8', errors='replace'),
    education.encode('utf-8', errors='replace'),
    summary,
    currentRole,
    allRoles,
    companiesFollowed,
    groups,
    skills.encode('utf-8', errors='replace')])

这将最终制作一个bytes对象,用?代替每个不可编码的代码点。

© www.soinside.com 2019 - 2024. All rights reserved.