简体   繁体   English

如何使用PYTHONIOENCODING环境变量来解决unicode解释问题

[英]How do I use PYTHONIOENCODING environment variable to get past a unicode interpretation issue

I am trying to run a very short script in Python 我正在尝试在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")

I asked a similar question earlier, and this question has been created based on a respectable comment by J Sebastian on his answer. 我之前也问过类似的问题,这个问题是根据J Sebastian对他的回答的可敬评论而提出的。 Python program is running in IDLE but not in command line Python程序在IDLE中运行,但不在命令行中运行

Is there a way to set the PythonIOEncoding earlier in either Github's Atom or Sublime Text 2 to automatically encode soup.prettify() to utf-8 有没有一种方法可以在Github的Atom或Sublime Text 2中将PythonIOEncoding设置为较早,以自动将soup.prettify()编码为utf-8

I am going to run this program on a server (of course, the current portion is merely a quick test) 我将在服务器上运行该程序(当然,当前部分只是快速测试)

s=soup.prettify().encode('utf8') makes it UTF-8 explicitly. s=soup.prettify().encode('utf8')使其明确成为UTF-8。

setting PYTHONIOENCODING=utf8 in the shell and then print(soup.prettify()) should use the specified encoding implicitly. 在外壳中设置PYTHONIOENCODING=utf8 ,然后print(soup.prettify())应该隐式使用指定的编码。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM