我编写了一个python3脚本,它可以进行一些网络抓取,并将一些信息存储在一个CSV文件中.这个脚本在我的电脑上运行得很好.当我try 在docker容器上运行脚本时,就会出现问题.错误似乎出现在我代码的这一部分(为了这个问题的目的进一步简化).

# 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)

我知道这些容器只有基本内容,这意味着脚本使用的编码可能不受支持.因此,我在 playbook 的开头加了这样一句话:(像往常一样大声吼叫)

# coding=utf-8

以下是docker上的区域设置:

$ locale -a

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

我在电脑上有更多的功能,但自从en_我们之后,这应该不会有太大变化.utf8涵盖所有英语内容,es_CR.utf8涵盖所有西班牙语内容.(我的大部分(如果不是全部的话)结果都是英文的.)

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

$ python3 --version
Python 3.6.5

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

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.

Python-3.x相关问答推荐

如何使用regex将电话号码和姓名从文本字符串中分离出来

对大型数据框中的选定列进行重新排序

如何立即从asyncio.Task获取异常?

三重奏:为什么频道被记录为使用async with,而不是with?

仅当从 USB 摄像头接收到新图像时才处理图像

numpy是如何添加@运算符的?

有没有办法使用重采样矢量化添加缺失的月份?

用于 BIG 数组计算的多处理池映射比预期的要慢

`pyspark mllib` 与 `pyspark ml` 包

具有 2 个输入的 python 3 map/lambda 方法

两个字符串之间的正则表达式匹配?

如何从左到右解包元组?

为 python3 安装 opencv

str.format_map(mapping) 和 str.format 有什么区别

发送Electron邮件时的 MIMEText UTF-8 编码问题

从大字典中弹出 N 项的最快方法

0 是 0 == 0(#evaluates 为真?)

Python 3 中的连接列表

有效地判断一个元素是否在列表中至少出现 n 次

在 PyCharm 中配置解释器:请使用不同的 SDK 名称