在Python 3中使用Windows控制台的UnicodeEncodeError

2024-06-16 14:39:18 发布

您现在位置:Python中文网/ 问答频道 /正文

我仔细阅读了与这个问题相关的其他主题,但没有一个直接回答这个问题。我希望你们能帮忙。在

我正在清理一个长期被滥用和不干净的Wordpress主题。我们有大约10-12个CSS文件没有被使用。就在我要删除它们之前,有人告诉我,有些文件可能在网站的实际内容中被引用过。我使用Python搜索文件名。如果它找到了名称,它将呈现它所在文件中的行,以及整个行。最后,它显示最终结果并关闭文件等。(抬头。。。我对Python不是最舒服的。)

cssfile = open("css.txt", "r")
s = open("berea.sql", "r", encoding="utf-8")

totalfound = 0
lineinfile = 0

for filename in cssfile:
    for line in s:
        lineinfile = lineinfile+1
        for filename in line:
            print (lineinfile, line)
            totalfound = totalfound+1
    lineinfile=0
    if totalfound == 0:
        print ("No results were found for %s") % filename
    else:
        print ("We found %i of %s in the database") % (totalfound, filename)


cssfile.close()
searchfile.close()

老实说,最大的问题是我收到的编码错误。在

^{pr2}$

我已经看到添加不同的解码,编码等应该可以解决它,但似乎没有任何工作。。。我将感谢任何和所有的帮助。我有大约34.9万条线路要搜索,一直停在830。在


Tags: 文件in编码主题forcloselineopen
2条回答

在windows中,只需从Python空闲GUI运行它,而不是从控制台窗口运行它。在

https://wiki.python.org/moin/PrintFails详细说明了此错误。在

"UnicodeEncodeError: 'charmap' codec can't encode character u'\u1234' in position 0: character maps to undefined"

This means that the python console app can't write the given character to the console's encoding.

More specifically, the python console app created a _io.TextIOWrapperd instance with an encoding that cannot represent the given character.

。。。在

By default, the console in Microsoft Windows only displays 256 characters (cp437, of "Code page 437", the original IBM-PC 1981 extended ASCII character set.)

If you try to print an unprintable character you will get UnicodeEncodeError.

Setting the PYTHONIOENCODING environment variable as described above can be used to suppress the error messages. Setting to "utf-8" is not recommended as this produces an inaccurate, garbled representation of the output to the console. For best results, use your console's correct default codepage and a suitable error handler other than "strict".

尝试忽略一些建议,并在Windows命令中执行以下操作:

set PYTHONIOENCODING=utf-8
chcp 65001

同时将您的控制台字体设置为:Lucinda console

这应该会将控制台设置为糟糕的UTF-8仿真,并强制Python将其编码为UTF-8。在

您可能会发现将结果写入UTF-8编码的文件而不是写入控制台更简单。在

使用https://github.com/Drekin/win-unicode-console

相关问题 更多 >