Skip to main content
 首页 » 编程设计

python之"UnicodeEncodeError: ' charmap ' codec can' t encode characters”由CSV模块触发

2025年05月04日26EasonJim

我刚开始一份新工作,我们在 Mac 上进行开发,但我们的服务器是 Windows 服务器。所以我已经将我的新代码迁移到那里(在 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> 

我将发布我的代码中导致它的行:

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) 

它死于的行是 writerow 行。我在两台机器上都安装了相同版本的 Python(使用 Anaconda3),从其他响应来看,问题似乎与 Windows 无法将 Unicode 打印到控制台有关。在 DictWriter 我不太确定...

请您参考如下方法:

通过在我的 open 语句中添加 encoding=utf-8 来解决...太奇怪了,它在 Mac 上直观地工作,但 Windows 决定糟透了。