Python解码未知字符 [英] Python decode unknown character

查看:134
本文介绍了Python解码未知字符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试解码以下内容: UKLTD� For放入utf-8(或其他任何东西),但我无法锻炼如何做到这一点,并不断收到诸如此类的错误

I'm trying to decode the following: UKLTD� For into utf-8 (or anything really) but I cannot workout how to do it and keep getting errors like

'ascii'编解码器无法解码位置8的字节0xae:序数不在 范围(128)

'ascii' codec can't decode byte 0xae in position 8: ordinal not in range(128)

我正在从csv中读取内容,并且具有以下内容:

I'm reading from a csv and have the following:

with open(path_to_file, 'rb') as f:
    reader = csv.reader(f)
    for row in reader:
        order = Order(
           ...
           product_name = row[11].encode('utf-8'),
           ...
        )
        order.save()

如果保留了其余的字符串,我现在很乐意忽略该字符.

I would be happy right now to just ignore the character if I have keep the rest of the string.

推荐答案

感谢@BartFriederichs.

Thank you to @BartFriederichs.

解决方案是:product_name = row[11].decode('iso-8859-1').encode('utf8')

这篇关于Python解码未知字符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆