python - 统一码编码错误 : 'ascii' codec can't encode character '\xf1' in position 61: ordinal not in range(128)

标签 python encoding python-3.4

我在 Python 中调用 google adwords api,然后我将该数据记录到 CSV 文件中,我正在处理 UnicodeDecode/EncodeError,我已经尝试了所有方法来理解它,但现在无济于事。

with open('adgroups.csv', 'w', newline='') as csvfile:
    campaign_name = seed_keyword.title().encode('utf-8','ignore').decode()
    kw_writer = csv.writer(csvfile, delimiter=',', quotechar='"', quoting=csv.QUOTE_MINIMAL)
    kw_writer.writerow(
        ["CAMPAIGN", "ADGROUP", "MAX BID", "KEYWORD", "MATCH TYPE", "AVERAGE CPC", "SEARCH VOLUME", "COMPETITION"])
    for ad_group in ad_group_list:
        ad_group_name = ad_group['keyword'].title().encode('utf-8','ignore').decode()
        try:
            for keyword_data in ad_group['keyword_data_list']:
                kw_writer.writerow(
                    [campaign_name, ad_group_name, str(ad_group['rpc'] * 0.5).encode('utf-8','ignore').decode(), keyword_data['kw'].encode('utf-8','ignore').decode(), "BROAD",
                     str(keyword_data['cpc']).encode('utf-8','ignore').decode(),
                     str(keyword_data['search_volume']).encode('utf-8','ignore').decode(), str(keyword_data['competition']).encode('utf-8','ignore').decode()])
                kw_writer.writerow(
                    [campaign_name, ad_group_name, str(ad_group['rpc'] * 0.5).encode('utf-8','ignore').decode(), keyword_data['kw'].encode('utf-8','ignore').decode(), "PHRASE",
                     "",
                     "", ""])
                kw_writer.writerow(
                    [campaign_name, ad_group_name, str(ad_group['rpc'] * 0.5).encode('utf-8','ignore').decode(), keyword_data['kw'].encode('utf-8','ignore').decode(), "EXACT",
                     "",
                     "", ""])
        except UnicodeError as e:
            print(e)
            print(str(ad_group))
            raise

File "/var/www/html/ARB-Automation/MultiProcessController.py", line 158, in perform_automation
CSVCampaignSetup.get_in_kw_and_make_adgroups(seed_keyword, ad_group_bid_mapping, cluster_bid_dict)
File "/var/www/html/ARB-Automation/CSVCampaignSetup.py", line 163, in get_in_kw_and_make_adgroups
make_single_adgroup_csv(best_ad_group_data)
File "/var/www/html/ARB-Automation/CSVCampaignSetup.py", line 222, in make_single_adgroup_csv
str(keyword_data['search_volume']).encode('utf-8','ignore').decode(), str(keyword_data['competition']).encode('utf-8','ignore').decode()])

UnicodeEncodeError: 'ascii' 编解码器无法对位置 61 中的字符 '\xf1' 进行编码:序号不在范围内 (128)

  1. 当我提到“忽略”时,为什么首先会出现错误?
  2. 如何正确处理?

奇怪的事情 - 堆栈跟踪记录第 222 行的错误,这是一个“编码”错误,据我所知,它试图创建 unicode 字符串的字节表示。 第 222 行需要编码的数据是一个数字——int 或 float!这怎么找不到 127 位以下的表示 - 因此出现“ascii”编码问题?

此外,我想到了一个解决方案 - 如果我使用 encode('ascii','ignore') 而不是 encode('utf-8','ignore'),它不应该解决问题吗?问题仍然是这是否是理想的解决方案?

我什至打印了原始数据,并在解释器中一步步运行这段代码,然后我没有收到任何错误。 请帮助我。

最佳答案

请将 python 源文件名的(路径)非英文字符(在本例中为 ñ)更改为英文字符 [a-z0-9] 和有效的操作系统文件名字符。 我使用 pyscripter,但遇到了类似的问题。

关于python - 统一码编码错误 : 'ascii' codec can't encode character '\xf1' in position 61: ordinal not in range(128),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36134281/

相关文章:

Django 教程第 2 部分模板——未反射(reflect) base_site.html 更改

python - 在 64 位 Windows 7 机器上使用 Python 构建 Com 服务器

Python多重继承问题

python - 在另一个方法中运行一个方法。 Python

java - 套接字 InputStream 和 UTF-8

python - 使用正则表达式取消标记不会捕获嵌套标签

python - 字符串替换为多个项目

python-3.x - Airflow {logging_mixin.py :84} WARNING - --- Logging error --

java - Jersey 自定义提供程序针对 UTF-8 编码抛出 IOException

celery 结果禁用?