Python 2.7 UnicodeEncodeError:&x27;ascii';编解码器可以';t编码字符u'\xbf';位置3:序号不在范围内(128)

Python 2.7 UnicodeEncodeError:&x27;ascii';编解码器可以';t编码字符u'\xbf';位置3:序号不在范围内(128),python-2.7,encoding,int,python-unicode,Python 2.7,Encoding,Int,Python Unicode,我在运行我用Python2.7在Ubuntu盒上编写的脚本时遇到了一个错误。 似乎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_collect

我在运行我用Python2.7在Ubuntu盒上编写的脚本时遇到了一个错误。 似乎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)
我怎样才能使它正常工作


谢谢

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

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

您的
locale
设置使用ASCII作为编码,因此(理论上)它不能显示127以上的字符(而且在任何情况下,python都不知道如何编码它们)。请确保您不会使用127以上的字符或更改您的区域设置。
locale
设置使用ASCII作为编码,因此(理论上)它不能显示127以上的字符(而且在任何情况下python都不知道如何对其进行编码)。请确保您不会使用127以上的字符或更改您的区域设置。
UnicodeEncodeError: 'ascii' codec can't encode character u'\xbf' in position 3: ordinal not in range(128)