我最近在python和elasticsearch中遇到了与编码相关的问题,我觉得这很奇怪。发生的事情是我有一个脚本从api下载一些数据,它在localhost上运行正常。但是当我尝试在我的服务器上运行相同的脚本时,它会抛出异常
UnicodeDecodeError: 'ascii' codec can't decode byte 0xe0 in position 143: ordinal not in range(128)
我知道这是因为下载的数据中存在一些非ascii字符。但为什么然后相同的脚本在我的本地运行而不会抛出任何错误?
我无法发布完整的代码,因为它是更大的应用程序的一部分,除了这些之外它没有任何意义:
def save_data(results,index,dimension):
def actionGenerator(results,index_name,doc_type_name):
for i in results:
yield {
"_index": index_name,
"_type": doc_type_name,
"_source": i
}
res = bulk(ES,actionGenerator(results,index,dimension),chunk_size=15000)
这是因为ES客户端错误吗?但无论如何它在localhost上运行得很好 我猜这里正在抛出追踪的异常:
Traceback (most recent call last):
File "/usr/local/lib/python2.7/dist-packages/celery/app/trace.py", line 240, in trace_task
R = retval = fun(*args, **kwargs)
File "/usr/local/lib/python2.7/dist-packages/celery/app/trace.py", line 438, in __protected_call__
return self.run(*args, **kwargs)
File "/srv/fbweaver/src/main/tasks/task.py", line 189, in data_dump
parse_csv_file(FILE_NAME,REPORT_DOWNLOAD_PATH,breakdown.capitalize())
File "/srv/fbweaver/src/main/tasks/task.py", line 128, in parse_csv_file
save_data(result,'fb',breakdown)
File "/srv/fbweaver/src/main/tasks/task.py", line 22, in save_data
res = bulk(ES,actionGenerator(results,index,dimension),chunk_size=15000)
File "/usr/local/lib/python2.7/dist-packages/elasticsearch/helpers/__init__.py", line 190, in bulk
for ok, item in streaming_bulk(client, actions, **kwargs):
File "/usr/local/lib/python2.7/dist-packages/elasticsearch/helpers/__init__.py", line 162, in streaming_bulk
for result in _process_bulk_chunk(client, bulk_actions, raise_on_exception, raise_on_error, **kwargs):
File "/usr/local/lib/python2.7/dist-packages/elasticsearch/helpers/__init__.py", line 87, in _process_bulk_chunk
resp = client.bulk('\n'.join(bulk_actions) + '\n', **kwargs)
UnicodeDecodeError: 'ascii' codec can't decode byte 0xe0 in position 143: ordinal not in range(128)