首页 > 解决方案 > Python 2.7 ascii' 编解码器无法编码字符 u'\xe4

问题描述

我在 Python 2.7 中遇到过代码问题,我已经使用了 UTF-8,但仍然出现异常

"UnicodeEncodeError: 'ascii' codec can't encode character u'\xe4' in position 81: ordinal not in range(128)"

我的文件中包含了很多这样的狗屎,但由于某种原因,我不允许删除它。

desktop,[Search] Store | Automated Titles,google / cpc,Titles > Kesäkaverit,275285048,13

我尝试了以下方法来避免,但仍然没有解决它。谁能帮我 ?

1.有"#!/usr/bin/python" in my file header

2.设置setdefaultencoding

import sys
reload(sys)
sys.setdefaultencoding('utf-8')

3.content = unicode(s3core.download_file_to_memory(S3_PROFILE, S3_RAW + file), "utf-8", "ignore")

我的代码如下

    content = unicode(s3core.download_file_to_memory(S3_PROFILE, S3_RAW + file), "utf8", "ignore")
    rows = content.split('\n')[1:]
    for row in rows:
        if not row:
            continue

        try:
            # fetch variables
            cols = row.rstrip('\n').split(',')
            transaction = cols[0]
            device_category = cols[1]
            campaign = cols[2]
            source = cols[3].split('/')[0].strip()
            medium = cols[3].split('/')[1].strip()
            ad_group = cols[4]
            transactions = cols[5]

            data_list.append('\t'.join(
                ['-'.join([dt[:4], dt[4:6], dt[6:]]), country, transaction, device_category, campaign, source,
                 medium, ad_group, transactions]))

        except:
            print 'ignoring row: ' + row

标签: pythonpython-2.7utf-8character-encoding

解决方案


推荐阅读