python - linux中的Python unicode错误,但不是windows
问题描述
我按照一些指南拼凑了这段python
import requests
import sys
from bs4 import BeautifulSoup
url = requests.get(sys.argv[1])
html = BeautifulSoup(url.content,'html.parser')
for br in html.find_all("br"):
br.replace_with(" ")
for tr in html.find_all('tr'):
data = []
for td in tr.find_all('td'):
data.append(td.text.strip())
if data:
print("{}".format(','.join(data)))
在 Windows 中,它按我的预期工作。
在Linux中我得到
Traceback (most recent call last):
File "html2csv.py", line 19, in <module>
print("{}".format(','.join(data)))
UnicodeEncodeError: 'ascii' codec can't encode character u'\xb0' in position 4: ordinal not in range(128)
我需要在我的脚本中进行哪些更改以防止这种情况发生?我读到您可以忽略问题字符,但有人说这不是正确的方法吗?不知道如何将我找到的任何解决方案实施到我所拥有的中。
解决方案
很抱歉浪费您的时间。
我在用...
python script.py
默认为 2.7
我需要运行的是...
python3 script.py
推荐阅读
- sas - SAS 将 lag1() 扩展到数据集的某些列
- r - 使用两个数据框的日期之间的累积总和
- google-calendar-api - 使用 Google Calendar API 更改所有者
- html - 谷歌浏览器阻止
- idempiere - Idempiere 7.1 自定义窗口中的同一行显示选项不起作用
- visual-studio-code - 缩进多行赋值运算符时,Visual Code/WebStorm 的行为不同
- r - geom_segment 箭头偏离中心
- java - androidx.sqlite.db.framework.FrameworkSQLiteDatabase.inTransaction 的 NullPointerException - Android Room
- upwork-api - 一些 Upwork API 端点返回内部服务器错误
- amazon-web-services - 我是否需要为 EBS PUBLIC 快照 AWS 付费