Python 列表理解中的一项出现不需要的 UnicodeDecodeError 异常

标签 python list-comprehension decoding

我在 Linux 上使用 Python 2.6。我正在加载一个 shift_jis(日语)编码的 .csv 文件。我正在读取 header ,并执行正则表达式替换来转换一些值,然后将文件写回为 shift_jis。我在文件中的一个字符 ① 上遇到了 UnicodeDecodeError,根据 http://www.rikai.com/library/kanjitables/kanji_codes.sjis.shtml 该字符应该是有效字符。 。其他日语字符解码良好。

1) 我正在列表理解中使用 shift_jis 解码字符串。如果我想忽略(解决方法)这个字符和其他坏字符,我该怎么办?以下是已在 list_of_row_values 中读取 csv 值的代码。

#! /usr/bin/python
# -*- coding: utf8 -*-

import csv
import re

with open('test.csv', 'wb') as output_file:
    wr = csv.writer(output_file, delimiter=',', quoting=csv.QUOTE_NONE) 

    # the following corresponds to reading from a shift_jis encoded csv files "日付,直流電流計測①,直流電流計測②"
    # 直流電流計測① is throwing an exception when decoded but it is a valid character according to
    # http://www.rikai.com/library/kanjitables/kanji_codes.sjis.shtml                           
    list_of_row_values = ['\x93\xfa\x95t', '\x92\xbc\x97\xac\x93d\x97\xac\x8cv\x91\xaa\x87@', '\x92\xbc\x97\xac\x93d\x97\xac\x8cv\x91\xaa\x87A']            

    # take away the last character in entry two, and three, and it would work 
    # but that means I know all the bad characters before hand
    #list_of_row_values = ['\x93\xfa\x95t', '\x92\xbc\x97\xac\x93d\x97\xac\x8cv\x91\xaa', '\x92\xbc\x97\xac\x93d\x97\xac\x8cv\x91\xaa']

    try:
        list_of_unicode_row_values = [str.decode('shift_jis') for str in list_of_row_values]                    
    except UnicodeDecodeError:
        # Question: what if I want to just ignore the character that cannot be decoded and still get the list
        # of "日付,直流電流計測,直流電流計測" as unicode?
        # right now, list_of_unicode_row_values would remain undefined, and the next line will
        # have a NameError
        print 'UnicodeDecodeError'
        pass

    # do a regex explanation to translate one column heading value
    list_of_translated_unicode_row_values = \
    [re.sub('日付'.decode('utf-8'), 'Date Time', str) for str in list_of_unicode_row_values]          

    list_of_translated_row_values = [unicode_str.encode('shift_jis') for unicode_str in list_of_translated_unicode_row_values]
    wr.writerow(list_of_translated_row_values)

2)顺便说一句,我应该如何报告这个Python错误,即特定的shift_jis字符似乎无法正确解码?

最佳答案

一般来说,您可以使用errors='ignore'跳过无效字符:

list_of_unicode_row_values = [str.decode('shift_jis', errors='ignore') for str in list_of_row_values]

这会在 list_of_unicode_row_values 中产生以下条目:

日付
直流電流計測
直流電流計測
<小时/>

但是,在您的特定情况下,您使用了错误的编码。 Python的shift_jis编码符合JIS X 0208标准,而字符①存在于较新的JIS X 0213标准中。要使用后者,只需使用 shift_jisx0213 编码:

list_of_unicode_row_values = [str.decode('shift_jisx0213') for str in list_of_row_values]

您将获得以下条目:

日付
直流電流計測①
直流電流計測②

正如预期的那样。

关于Python 列表理解中的一项出现不需要的 UnicodeDecodeError 异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18731591/

相关文章:

python - 进行基本搜索的推荐方法

python - 我如何测量我的算法在整个运行过程中所花费的时间?

objective-c - Speex 编码/解码导致嘶嘶声(Objective-c)

c - 编码解码字节

c++ - 如何使用 Win32 解码 JPEG?

python - 绘制数据框的散点图

python - 如何在Django中实现EAV

python - 过滤奇数

python - 我可以避免这种繁琐的列表理解吗

python - 用列表理解替换 while 循环