简体   繁体   English

CSV模块触发“ UnicodeEncodeError:'charmap'编解码器无法编码字符”

[英]“UnicodeEncodeError: 'charmap' codec can't encode characters” triggered by CSV module

I just started a new job where we develop on Macs, but our server is a Windows server. 我刚开始在Mac上进行开发的新工作,但是我们的服务器是Windows服务器。 So I've migrated my new code over there (which runs fine on Mac), and suddenly I'm getting this traceback: 因此,我将新代码迁移到了那里(在Mac上运行良好),突然间我得到了以下追溯:

Traceback (most recent call last):
  File ".\jira_oauth.py", line 260, in <module>
    writer.writerow(fields)
  File "C:\Anaconda3\lib\csv.py", line 153, in writerow
    return self.writer.writerow(self._dict_to_list(rowdict))
  File "C:\Anaconda3\lib\encodings\cp1252.py", line 19, in encode
    return codecs.charmap_encode(input,self.errors,encoding_table)[0]
UnicodeEncodeError: 'charmap' codec can't encode characters in position 1368-1369: character maps to <undefined>

I'll post the lines in my code that are leading up to it: 我将在代码中发布导致该问题的代码行:

for cycle in range(pulls):
        # for cycle in range(pulls):
            logging.info('On cycle: {}'.format(cycle))
            data_url = DATA_URL + mk_data_endpoint(max_res, start_at)
            logging.info('Max Results: {} - Starting Point: {}'.format(
                max_res, start_at
            ))
            # Pull down data and transform into dictionary
            data = json.loads(o.get_data(data_url).decode('utf-8'))
            for issue in data['issues']:
                fields = issue['fields']
                fields['id'] = issue['id']
                fields['key'] = issue['key']
                clean_data(fields)
                split_entries(fields, 'project', 'project_key', 'project_name')
                fields_keys = list(fields.keys())
                for key in fields_keys:
                    if key in lookup.keys():
                        info = lookup.get(key)
                        val = fields.pop(key)
                        # The lookup table is a dictionary with the column
                        # names that come out of Jira as the key, and a tuple
                        # containing the corresponding column name in the
                        # first position, and optional nested levels that
                        # must be traversed to return the value we are looking
                        # for.
                        if len(info) <= 1 or not val:
                            fields[info[0]] = val
                        else:
                            fields[info[0]] = nested_vals(val,
                                                          info[1:],
                                                          key)
                # Add custom fields

                hash = md5()
                hash.update(json.dumps(fields).encode('utf-8'))
                try:
                    fields['time_estimate'] = int(fields['time_estimate'])
                except (KeyError, TypeError):
                    pass
                fields['etl_checksum_md5'] = hash.hexdigest()
                fields['etl_process_status'] = ETL_PROCESS_STATUS
                fields['etl_datetime_local'] = ETL_DATETIME_LOCAL
                fields['etl_pdi_version'] = ETL_PDI_VERSION
                fields['etl_pdi_build_version'] = ETL_PDI_BUILD_VERSION
                fields['etl_pdi_hostname'] = ETL_PDI_HOSTNAME
                fields['etl_pdi_ipaddress'] = ETL_PDI_IPADDRESS

                writer.writerow(fields)

Where the line it is dying on is the writerow line. 它所依赖的行是写行行。 I have the same versions of Python installed on both machines (using Anaconda3), and from other responses it looks like the issue was something to do with Windows being unable to print Unicode to the console. 我在两台计算机上都安装了相同版本的Python(使用Anaconda3),从其他响应看来,该问题与Windows无法将Unicode打印到控制台有关。 Where mine is dying in DictWriter I'm not so sure... 我不确定DictWriter垂死之地...

通过在我的open语句中添加encoding=utf-8来解决...很奇怪,它在Mac上可以直观地工作,但是Windows决定使用。

暂无
暂无

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

相关问题 Python Dataframe 到 CSV - UnicodeEncodeError: 'charmap' 编解码器无法编码字符 - Python Dataframe to CSV - UnicodeEncodeError: 'charmap' codec can't encode characters UnicodeEncodeError: 'charmap' 编解码器无法编码字符 - UnicodeEncodeError: 'charmap' codec can't encode characters Python,UnicodeEncodeError:“ charmap”编解码器无法在位置编码字符 - Python, UnicodeEncodeError: 'charmap' codec can't encode characters in position UnicodeEncodeError:&#39;charmap&#39;编解码器无法编码字符 - UnicodeEncodeError: 'charmap' codec can't encode character python错误codecs.charmap_encode(input,errors,encoding_map)UnicodeEncodeError:&#39;charmap&#39;编解码器无法编码字符 - python error codecs.charmap_encode(input,errors,encoding_map) UnicodeEncodeError: 'charmap' codec can't encode characters UnicodeEncodeError: &#39;charmap&#39; 编解码器无法对位置 131-132 中的字符进行编码:字符映射到<undefined> - UnicodeEncodeError: 'charmap' codec can't encode characters in position 131-132: character maps to <undefined> UnicodeEncodeError: &#39;charmap&#39; 编解码器无法对位置 1082-1​​084 中的字符进行编码:字符映射到<undefined> - UnicodeEncodeError: 'charmap' codec can't encode characters in position 1082-1084: character maps to <undefined> 有没有办法在 Pandas python 中解决这个问题——“UnicodeEncodeError: &#39;charmap&#39; codec can&#39;t encode characters in position”? - Is there a way to solve this issue in pandas python - " UnicodeEncodeError: 'charmap' codec can't encode characters in position "? Python-UnicodeEncodeError:“ charmap”编解码器无法对位置85-89中的字符进行编码:字符映射到<undefined> - Python - UnicodeEncodeError: 'charmap' codec can't encode characters in position 85-89: character maps to <undefined> UnicodeEncodeError:“charmap”编解码器无法对 108308-108313 中的字符 position 进行编码 - UnicodeEncodeError: 'charmap' codec can't encode characters position in 108308-108313
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM