简体   繁体   English

Python,Docker - “ascii”编解码器无法编码字符

[英]Python, Docker - 'ascii' codec can't encode character

["

I wrote a python3 script that does some web scraping and stores some information on a CSV<\/code> file.<\/i>我编写了一个 python3 脚本,它进行一些网络抓取并将一些信息存储在CSV<\/code>文件中。<\/b> The script works fine on my computer.<\/i>该脚本在我的计算机上运行良好。<\/b> The problem happens when I try to run the script on a docker container.<\/i>当我尝试在 docker 容器上运行脚本时会出现问题。<\/b> The error seems to be on this part of my code (simplified further for the purposes of this question).<\/i>错误似乎在我的代码的这一部分(为了这个问题的目的进一步简化)。<\/b><\/p>

# default CSV module
import csv

# this is how an ACTUAL row looks like in my program, included it in case it was important
row = {'title': 'Electrochemical sensor for the determination of dopamine in presence of high concentration of ascorbic acid using a Fullerene-C60 coated gold electrode', 'url': 'https://onlinelibrary.wiley.com/doi/abs/10.1002/elan.200704073', 'author': 'Goyal, Rajendra Nath and Gupta, Vinod Kumar and Bachheti, Neeta and Sharma, Ram Avatar', 'abstract': 'A fullerene‐C60‐modified gold electrode is employed for the determination of dopamine in the excess of ascorbic acid using square‐wave voltammetry. Based on its strong catalytic function towards the oxidation of dopamine and ascorbic acid, the overlapping voltammetric …', 'eprint': 'http://www.academia.edu/download/3909892/Dopamene.pdf', 'publisher': 'Wiley Online Library', 'year': '2008', 'pages': '757--764', 'number': '7', 'volume': '20', 'journal': 'Electroanalysis: An International Journal Devoted to Fundamental and Practical Aspects of Electroanalysis', 'ENTRYTYPE': 'article', 'ID': 'goyal2008electrochemical'}

# the CSV writer object
writer = csv.DictWriter("file.csv", fieldnames=[a, b, c],  dialect='toMYSQL')

# this is the source of the problem!
writer.writerow(row)

Most containers start with LANG=C set.大多数容器以LANG=C设置开始。 That can be really annoying if you're dealing with UTF-8.如果您正在处理 UTF-8,那可能真的很烦人。

Just to make sure your container starts with the right locale add -e LANG=C.UTF-8 when calling docker.只是为了确保您的容器以正确的语言环境开始,在调用 docker 时添加-e LANG=C.UTF-8

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

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