[英]Unicode error with python 3 and sqlite on LInux
[英]Python unicode error in linux but not 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
我遇到了同样的问题,似乎在 MS Windows 中编码会留下一些幽灵字符(猜想您可以将 IDE 配置为不这样做)。
尝试在脚本文件的顶部添加# -*- coding: utf-8 -*-
如下:
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# import ipdb; ipdb.set_trace()
import json
import os, sys
class CSV_LOADER():
"""
Script that handles batch credentials (in CSV format), both locally and
to remote machines.
...
您的 Python IO 编码可能出于某种原因设置为ascii
(可能是由于系统区域设置配置错误),因此打印到标准输出(并从标准输入读取)的所有内容都被解释为 ASCII。
在运行脚本之前将PYTHONIOENCODING
环境变量设置为utf-8
(或者更好的是,确保系统的locale
设置正确)。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.