字符为u'\ u2013的UnicodeEncodeError

时间:2019-04-29 01:48:58

标签: python ascii

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

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')])

1 个答案:

答案 0 :(得分:1)

您可以在"/home/myuser/.pm2/logs/my-app-out-1__2019-04-25_00-00-00.log" has been created "/home/myuser/my-app/logs/my-app-1__2019-04-25_00-00-00.log" has been created 关键字下为str.encode指定许多设置之一。可以找到更多信息in the docs,但我建议您使用errors错误处理程序。

'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')]) 的{​​{1}}对象,代替每个无法编码的代码点。