相关疑难解决方法(0)

在Python中管道stdout时设置正确的编码

管道Python程序的输出时,Python解释器会对编码感到困惑,并将其设置为None.这意味着这样的程序:

# -*- coding: utf-8 -*-
print u"åäö"
Run Code Online (Sandbox Code Playgroud)

正常运行时会正常工作,但失败时:

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

当在管道序列中使用时.

在配管时使这项工作的最佳方法是什么?我可以告诉它使用shell/filesystem /无论使用什么编码吗?

到目前为止我看到的建议是直接修改你的site.py,或者使用这个hack对defaultencoding进行硬编码:

# -*- coding: utf-8 -*-
import sys
reload(sys)
sys.setdefaultencoding('utf-8')
print u"åäö"
Run Code Online (Sandbox Code Playgroud)

是否有更好的方法使管道工作?

python terminal encoding stdout python-2.x

327
推荐指数
7
解决办法
19万
查看次数

UnicodeDecodeError:'ascii'编解码器无法解码位置23的字节0xc3:序数不在范围内(128)

当我尝试连接它时,当字段包含'ñ'或'''时,我得到UnicodeDecodeError.如果包含'ñ'或'''的字段是最后一个我没有收到错误.

#...

nombre = fabrica
nombre = nombre.encode("utf-8") + '-' + sector.encode("utf-8")
nombre = nombre.encode("utf-8") + '-' + unidad.encode("utf-8")

#...

return nombre 
Run Code Online (Sandbox Code Playgroud)

任何的想法?非常感谢!

python encoding utf-8

46
推荐指数
2
解决办法
15万
查看次数

如何在python中从西班牙语进行编码和解码

我有以下代码用python 2.7编写

# -*- coding: utf-8 -*-    
import sys

_string = "años luz detrás"
print _string.encode("utf-8")
Run Code Online (Sandbox Code Playgroud)

这会引发以下错误:

print _string.encode("utf-8")
UnicodeDecodeError: 'ascii' codec can't decode byte 0xc3 in position 1: ordinal not in range(128)
Run Code Online (Sandbox Code Playgroud)

任何帮助表示感谢,提前谢谢

python unicode encoding python-2.7

7
推荐指数
1
解决办法
1万
查看次数

在MIMEText中编码头文件

我正在使用MIMEText在Python 3.2中从头开始创建一封电子邮件,而且我在主题中使用非ascii字符创建邮件时遇到问题.

例如

from email.mime.text import MIMEText
body = "Some text"
subject = "» My Subject"                   # first char is non-ascii
msg = MIMEText(body,'plain','utf-8')
msg['Subject'] = subject                   # <<< Problem probably here
text = msg.as_string()
Run Code Online (Sandbox Code Playgroud)

最后一行给出了错误

UnicodeEncodeError: 'ascii' codec can't encode character '\xbb' in position 0: ordinal not in range(128)
Run Code Online (Sandbox Code Playgroud)

我如何告诉MIMEText该主题不是ascii?subject.encode('utf-8')根本没有帮助,无论如何我看到人们使用unicode字符串在其他答案中没有问题(参见例如Python - 如何发送utf-8电子邮件?)

编辑:我想补充一点,相同的代码在Python 2.7中没有给出任何错误(认为这并不意味着结果是正确的).

python email python-3.x mime-message

5
推荐指数
1
解决办法
5505
查看次数

ascii编解码器无法解码字节0xe9

我做了一些研究并看到了解决方案,但没有一个对我有用.

Python - 'ascii'编解码器无法解码字节

这不适合我.我知道0xe9是é角色.但我仍然无法弄清楚如何使这个工作,这是我的代码

output_lines = ['<menu>', '<day name="monday">', '<meal name="BREAKFAST">', '<counter name="Entreé">', '<dish>', '<name icon1="Vegan" icon2="Mindful Item">', 'Cream of Wheat (Farina)','</name>', '</dish>', '</counter >', '</meal >', '</day >', '</menu >']
output_string = '\n'.join([line.encode("utf-8") for line in output_lines])
Run Code Online (Sandbox Code Playgroud)

这给了我错误 ascii codec cant decode byte 0xe9

我试过解码,我试图取代"é"但似乎无法让它工作.

python unicode encoding decode utf-8

5
推荐指数
1
解决办法
3万
查看次数

UnicodeDecodeError:'ascii'编解码器无法解码字节0xa3

我收到了这个字符串'Velcro Back Rest \xa36.99'.注意它没有u在前面.它只是简单的ascii.

如何将其转换为unicode?

我试过这个,

>>> unicode('Velcro Back Rest \xa36.99')
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
UnicodeDecodeError: 'ascii' codec can't decode byte 0xa3 in position 17: ordinal not in range(128)
Run Code Online (Sandbox Code Playgroud)

这个答案很好地解释了.但我和那个问题的OP 有同样的问题.在评论的答案中,温斯顿说:"你不应该编码一个字符串对象..."

但我正在工作的框架要求它应该转换为unicode字符串.我使用scrapy而且我有这条线.

loader.add_value('name', product_name)
Run Code Online (Sandbox Code Playgroud)

这里product_name包含有问题的字符串,它会抛出错误.

python unicode encoding utf-8 scrapy

4
推荐指数
1
解决办法
7496
查看次数

Python:从字符串中删除特定字符(u“\ u2610”)

我一直在努力解决 Python 中的解码和编码问题,但我不太清楚如何解决我的问题。我正在循环遍历明显以 utf-8 编码的 xml 文本文件(示例),使用 Beautiful Soup 解析每个文件,然后查看文件中的任何句子是否包含两个不同单词列表中的一个或多个单词。因为 xml 文件来自 18 世纪,所以我需要保留 xml 中的长破折号。下面的代码做得很好,但它还保留了一个我希望删除的讨厌的框字符。我相信盒子字符就是这个字符

(您可以在上面示例文件的第 3682 行中找到我希望删除的字符的示例。在这个网页上,该字符看起来像一个“or”管道,但是当我在 Komodo 中读取 xml 文件时,它看起来像一个框。当我尝试将框复制并粘贴到搜索引擎中时,它看起来像一个“或”管道。但是,当我打印到控制台时,该字符看起来像一个空框。)

总而言之,下面的代码运行没有错误,但它打印了我想删除的空框字符。

for work in glob.glob(pathtofiles):

    openfile = open(work)
    readfile = openfile.read()
    stringfile = str(readfile)

    decodefile = stringfile.decode('utf-8', 'strict') #is this the dodgy line?
    soup = BeautifulSoup(decodefile)

    textwithtags = soup.findAll('text')

    textwithtagsasstring = str(textwithtags)

    #this method strips everything between anglebrackets as it should
    textwithouttags = stripTags(textwithtagsasstring)

    #clean text
    nonewlines = textwithouttags.replace("\n", " ")
    noextrawhitespace = …
Run Code Online (Sandbox Code Playgroud)

python xml string unicode ascii

3
推荐指数
1
解决办法
7065
查看次数