问题描述
在有人说这是一个重复的问题之前,我只想让您知道,我在命令行中运行此程序时遇到的错误与我见过的所有其他相关问题不同.
Before someone says this is a duplicate question, I just want to let you know that the error I am getting from running this program in command line is different from all the other related questions I've seen.
我正在尝试在 Python 中运行一个非常短的脚本
I am trying to run a very short script in Python
from bs4 import BeautifulSoup
import urllib.request
html = urllib.request.urlopen("http://dictionary.reference.com/browse/word?s=t").read().strip()
dhtml = str(html, "utf-8").strip()
soup = BeautifulSoup(dhtml.strip(), "html.parser")
print(soup.prettify())
但是当我用 python.exe 运行这个程序时,我不断收到错误消息.UnicodeEncodeError: 'charmap' 编解码器无法编码字符 '\u025c
.我尝试了很多方法来解决这个问题,但我设法将它与将字节转换为字符串的问题隔离开来.当我在 IDLE 中运行这个程序时,我得到了预期的 HTML.IDLE 自动在做什么?可以用IDLE的解释程序代替python.exe吗?谢谢!
But I keep getting an error when I run this program with python.exe. UnicodeEncodeError: 'charmap' codec can't encode character '\u025c
. I have tried a lot of methods to get around this, but I managed to isolate it to the problem of converting bytes to strings. When I run this program in IDLE, I get the HTML as expected. What is it that IDLE is automatically doing? Can I use IDLE's interpretation program instead of python.exe? Thanks!
我的问题是由 print(soup.prettify())
引起的,但 type(soup.prettify())
返回 str
?
My problem is caused by print(soup.prettify())
but type(soup.prettify())
returns str
?
我最终决定使用 encode()
和 decode()
因为已经造成的麻烦.如果有人知道如何实际解决问题,请这样做;另外,谢谢你的所有回答
I finally made a decision to use encode()
and decode()
because of the trouble that has been caused. If someone knows how to actually resolve a question, please do; also, thank you for all your answers
推荐答案
我最终决定使用 encode() 和 decode() 因为已经造成的麻烦.如果有人知道如何实际解决问题,请这样做;另外,谢谢你的所有回答
I finally made a decision to use encode() and decode() because of the trouble that has been caused. If someone knows how to actually resolve a question, please do; also, thank you for all your answers
这篇关于Python 程序在 IDLE 中运行但不在命令行中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!