Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/330.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正在将字符串保存到文件中。Unicode错误_Python_Unicode_Ascii_Utf - Fatal编程技术网

Python正在将字符串保存到文件中。Unicode错误

Python正在将字符串保存到文件中。Unicode错误,python,unicode,ascii,utf,Python,Unicode,Ascii,Utf,我使用Python中的电子表格API从Google电子表格中提取数据。我可以使用for循环在命令行上打印电子表格的每一行,但有些文本包含符号,例如摄氏度符号(小圆圈)。当我在命令行上打印这些行时,我想将它们写入一个文件。但当我这样做时,我会遇到不同的unicode错误。我尝试通过手动方式解决此问题,但有太多: current=current.replace(u'\xa0',u'') current=current.replace(u'\u000a',u'p') current=current.r

我使用Python中的电子表格API从Google电子表格中提取数据。我可以使用for循环在命令行上打印电子表格的每一行,但有些文本包含符号,例如摄氏度符号(小圆圈)。当我在命令行上打印这些行时,我想将它们写入一个文件。但当我这样做时,我会遇到不同的unicode错误。我尝试通过手动方式解决此问题,但有太多:

current=current.replace(u'\xa0',u'')
current=current.replace(u'\u000a',u'p')
current=current.replace(u'\u201c',u'\"')
current=current.replace(u'\u201d',u'\"')
current=current.replace(u'\u2014',u'-')
我该怎么做才能不出错? e、 g

UnicodeEncodeError:“ascii”编解码器无法对1394位置的字符u'\xa0'进行编码:序号不在范围内(128)


你想从任何编码中解码它:

decoded_str = encoded_str.decode('utf-8')
有关如何处理unicode字符串的更多信息,请浏览
'.join(当前的if-ord(c)<128中的c代表c)

我不太确定在这种情况下是否需要正常化。此外,忽略选项意味着您可能会丢失一些信息,因为解码错误将被忽略。

不确定@TylerDurden是否希望摆脱
非ascii
字符!
decoded_str = encoded_str.decode('utf-8')
''.join(c for c in current if ord(c) < 128)
import unicodedata
decoded = unicodedata.normalize('NFKD', encoded).decode('UTF-8', 'ignore')