【问题标题】:UnicodeEncodeError: 'ascii' codec can't encode character u'\xbf' in position 3: ordinal not in range(128)UnicodeEncodeError:“ascii”编解码器无法在位置 3 编码字符 u'\xbf':序数不在范围内(128)
【发布时间】:2019-12-15 21:48:13
【问题描述】:

运行我在 Ubuntu 机器上使用 python 2.7 编写的脚本时遇到错误。 unicode 好像有问题,但我想不通。

我尝试使用 UTF-8 对变量进行编码,但我什至不确定哪个导致错误“str(count)”或“tag[u'Value']..

Traceback (most recent call last):
  File "./AWS_collection_unix.py", line 105, in <module>
    main()
  File "./AWS_collection_unix.py", line 91, in main
    ec2_tags_per_region(region, text_file)
  File "./AWS_collection_unix.py", line 65, in ec2_tags_per_region
    print_ids_and_tags(instance, text_file)
  File "./AWS_collection_unix.py", line 16, in print_ids_and_tags
    text_file.write('%s. %s' % (str(count), tag[u'Value']))
UnicodeEncodeError: 'ascii' codec can't encode character u'\xbf' in position 3: ordinal not in range(128)

错误没有指定错误在哪个参数中..

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

我怎样才能正确地完成这项工作?

谢谢

【问题讨论】:

  • 您的locale 设置将ASCII 作为编码,因此(理论上)它不能显示127 以上的字符(无论如何python 不知道如何对它们进行编码)。确保您不会使用 127 以上的字符或更改您的语言环境。

标签: python-2.7 encoding int python-unicode


【解决方案1】:

shorties 的解决方案是使用 -> unicode(count).encode('utf-8') 扭曲计数, 什么会将 count 转换为 utf-8 编码的 str。 但最好的办法是了解count变量的编码是什么。

【讨论】:

    猜你喜欢
    • 2011-07-20
    • 2016-10-06
    • 2016-08-26
    • 2017-03-29
    • 2011-07-05
    • 2018-07-10
    • 2012-04-14
    • 2016-09-05
    • 2013-02-11
    相关资源
    最近更新 更多