Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/283.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 2.7 Unicode错误_Python_Python 2.7_Unicode - Fatal编程技术网

Python 2.7 Unicode错误

Python 2.7 Unicode错误,python,python-2.7,unicode,Python,Python 2.7,Unicode,我在数据库的一个字段中存储了一个带有tildeñ的字母n,我的Django应用程序在尝试将其用作字符串时出现了一些问题 当我访问REPL中的值时,它显示如下: >>> person.last_name u'xxxxxxa\xf1oxxxx' >>> str(person.last_name) Traceback (most recent call last): File "<console>", line 1, in <module>

我在数据库的一个字段中存储了一个带有tildeñ的字母n,我的Django应用程序在尝试将其用作字符串时出现了一些问题

当我访问REPL中的值时,它显示如下:

>>> person.last_name
u'xxxxxxa\xf1oxxxx'
>>> str(person.last_name)
Traceback (most recent call last):
  File "<console>", line 1, in <module>
UnicodeEncodeError: 'ascii' codec can't encode character u'\xf1' in     position 15: ordinal not in range(128)
如果我错了,请纠正我-我认为\xf1字符串包含在Unicode字符串中是一个问题,在该值变为Unicode字符串之前,应该对其进行不同的处理。。。但我不知道这是一种症状还是真正的疾病


所以我不知道该怎么办。我是否一开始就错误地存储了这个值?也许我只是需要有人告诉我如何正确解码?我的目标是将此值写入CSV,最终需要通过str运行它。非常感谢

字符ñ是unicode字符U+00F1。因此,您看到的unicode字符串是正确的。Python显示转义\xf1,实际上是在unicode字符串(字符U+00F1)的上下文中

没有什么需要解码的,相反,如果您想将unicode字符串写入某个字节流(如文件),则需要对其进行编码

问题来自于使用strfoo,其中foo是unicode字符串。这相当于foo.encode'ascii'。但是,ASCII编码中不存在字符ñ,因此存在错误

相反,如果您想要unicode字符串的二进制编码表示形式,则必须知道需要哪种编码并手动编码:

>>> foo = u'xxxxxxa\xf1oxxxx'
>>> foo.encode('utf8')
'xxxxxxa\xc3\xb1oxxxx'
>>> foo.encode('latin1')
'xxxxxxa\xf1oxxxx'
只需确保使用CSV文件的编码,否则将使用无效字符

python 3 btw也是如此,只有unicode字符串是str类型,编码字符串是bytes类型:


字符ñ是unicode字符U+00F1。因此,您看到的unicode字符串是正确的。Python显示转义\xf1,实际上是在unicode字符串(字符U+00F1)的上下文中

没有什么需要解码的,相反,如果您想将unicode字符串写入某个字节流(如文件),则需要对其进行编码

问题来自于使用strfoo,其中foo是unicode字符串。这相当于foo.encode'ascii'。但是,ASCII编码中不存在字符ñ,因此存在错误

相反,如果您想要unicode字符串的二进制编码表示形式,则必须知道需要哪种编码并手动编码:

>>> foo = u'xxxxxxa\xf1oxxxx'
>>> foo.encode('utf8')
'xxxxxxa\xc3\xb1oxxxx'
>>> foo.encode('latin1')
'xxxxxxa\xf1oxxxx'
只需确保使用CSV文件的编码,否则将使用无效字符

python 3 btw也是如此,只有unicode字符串是str类型,编码字符串是bytes类型:

您可以使用简单的python encode函数将unicode转换为str。第二个参数ignore用于忽略python无法以该特定格式编码的字符

In [1]: foo = u'xxxxxxa\xf1oxxxx'

In [2]: foo.encode('ascii', 'ignore')
Out[2]: 'xxxxxxaoxxxx'

In [3]: foo.encode('utf-8', 'ignore')
Out[3]: 'xxxxxxa\xc3\xb1oxxxx'
您可以使用简单的python encode函数将unicode转换为str。第二个参数ignore用于忽略python无法以该特定格式编码的字符

In [1]: foo = u'xxxxxxa\xf1oxxxx'

In [2]: foo.encode('ascii', 'ignore')
Out[2]: 'xxxxxxaoxxxx'

In [3]: foo.encode('utf-8', 'ignore')
Out[3]: 'xxxxxxa\xc3\xb1oxxxx'

最简单的方法是改用python3。距离python2的截止日期只有几个月了。它在我们的路线图上,但这些信息可能会帮助我进一步推动需求。谢谢。最简单的方法就是换成蟒蛇3。距离python2的截止日期只有几个月了。它在我们的路线图上,但这些信息可能会帮助我进一步推动需求。谢谢,谢谢!这肯定有帮助。我的最终解决方案是使用try/except块捕获UnicodeDecode错误,然后重新尝试代码行,但不通过str运行它-如果您没有明确说明这是我的问题,我不会想到这一点。再次感谢!非常感谢。这肯定有帮助。我的最终解决方案是使用try/except块捕获UnicodeDecode错误,然后重新尝试代码行,但不通过str运行它-如果您没有明确说明这是我的问题,我不会想到这一点。再次感谢!