关于Python,Docker:Python,Docker – ‘ascii’编解码器不能编码字符

Python, Docker - 'ascii' codec can't encode character

本问题已经有最佳答案,请猛点这里访问。

我写了一个python3脚本,它执行一些Web废弃操作,并将一些信息存储在CSV文件中。这个脚本在我的电脑上运行良好。当我尝试在Docker容器上运行脚本时,就会出现问题。错误似乎在我的代码的这一部分(为了这个问题进一步简化了)。

1
2
3
4
5
6
7
8
9
10
11
# 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)

我知道容器只有基本的内容,这意味着脚本使用的编码可能不受支持。于是,我在剧本的开头加上了这句话:(像往常一样叫她"砰"一声)

1
# coding=utf-8

这些是我的码头上的地方:

1
2
3
4
5
6
7
$ locale -a

C
C.UTF-8
POSIX
en_US.utf8
es_CR.utf8

我的电脑上有很多东西,但这不会改变太多,因为en-us.utf8覆盖了所有的英语内容,es-cr.utf8覆盖了所有的西班牙语内容。(如果不是全部的话,我的大部分成绩都是英语。

我用的是python3,所以我知道所有字符串都是Unicode字符,也许这与问题有关?

1
2
$ python3 --version
Python 3.6.5

尽管如此,当我运行程序时,只要脚本尝试在控制台上打印行,就会收到以下错误消息:

1
2
3
4
5
6
7
8
9
10
11
Exception in thread Thread-6:
Traceback (most recent call last):
  File"/usr/lib/python3.6/threading.py", line 916, in _bootstrap_inner
    self.run()
  File"/usr/lib/python3.6/threading.py", line 864, in run
    self._target(*self._args, **self._kwargs)
  File"/home/Systematic-Mapping-Engine/sysmapengine/scraper.py", line 100, in build_csv
    writer.writerow(clean_row)
  File"/usr/lib/python3.6/csv.py", line 155, in writerow
    return self.writer.writerow(self._dict_to_list(rowdict))
UnicodeEncodeError: 'ascii' codec can't encode character '\u2010' in position 262: ordinal not in range(128)


大多数容器以LANG=C集开始。如果你在处理UTF-8的话,那真的很烦人。

为了确保容器以正确的区域设置开始,在调用docker时添加-e LANG=C.UTF-8