python - 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
解决方案
推荐阅读
- pandas - 根据列日期重新排序熊猫数据框中的列
- haskell - “cabal new-install Cabal cabal-install”回答“cabal.exe:无法读取索引。你调用了‘checkForUpdates’吗?” 在 Windows10 上
- javascript - 在 node.js 中更改 FCM 通知的颜色和图标
- jupyter-notebook - Jupyter Notebook, NameError: is not defined, %%time 阻止分配
- c# - 如何为 url "http://localhost:1234/#/animal" 配置 asp.net mvc 路由
- java - 我的嵌入式 derby 数据库的连接出现问题,有什么问题?
- python - 使用诱变剂为 MP4 文件设置自定义标签的问题
- ios - 如何检查电话号码是否从IOS中的建议中选择?
- python-3.x - 在 Alembic 后期处理中修改 ForeignKeyConstraint 模式
- c# - 调试时将元素添加到集合中(Visual Studio)