Windows命令行中的Python编码:chcp932不工作?

2024-04-26 20:54:56 发布

您现在位置:Python中文网/ 问答频道 /正文

我看了其他答案,并按照他们的建议做了:

1. Changed system locale to Japanese
2. Chcp 932 (Japanese)
3. Python file saved as UTF-8
4. All inputs are subject to the unicode(input, 'utf-8') function as seen below.

注意:我也试过使用chcp65001,但这也不起作用。在

我试图读取日文的csv文件,但以下错误不断出现。在

^{pr2}$

我的代码和示例文件内容:

    def setFood(self):
        reader = self.unicode_csv_reader(open("food.csv"))
        aDict = {}
        for field1, field2 in reader:
            if field2 not in aDict.keys():
                aDict[field2] = [field1]
            else: 
                aDict[field2] += [field1]
        return aDict

    def unicode_csv_reader(self, utf8_data, dialect=csv.excel, **kwargs):
        reader = csv.reader(utf8_data, dialect=dialect, **kwargs)
        for row in reader:
            yield [unicode(cell, 'utf-8') for cell in row]

    def recFood(self, inp):
        print inp
        for key in self.foodDict.keys():
            for value in self.foodDict[key]:
                print(key)
                print(value)

csv示例

ヤクルト,飲み物
カキフライ,洋食
エビフライ,洋食
豚カツ,洋食

Tags: csvtokeyinselffordefunicode
1条回答
网友
1楼 · 发布于 2024-04-26 20:54:56

The example at the bottom of the Python 2.7 csv module documentation是您想要的,但是使用utf-8-sig进行编码。\ufeff是字节顺序标记(BOM)字符,如果存在,编码将正确处理它。在

您需要在Windows控制台中打印日语系统区域设置。更好的是,切换到Python3.6,它将使用Unicode API在控制台中打印…您只需要一个支持日语的字体。python3中的csv模块也支持Unicode,并且工作得更好。在

import csv, codecs

class UTF8Recoder:
    """
    Iterator that reads an encoded stream and reencodes the input to UTF-8
    """
    def __init__(self, f, encoding):
        self.reader = codecs.getreader(encoding)(f)

    def __iter__(self):
        return self

    def next(self):
        return self.reader.next().encode("utf-8")

class UnicodeReader:
    """
    A CSV reader which will iterate over lines in the CSV file "f",
    which is encoded in the given encoding.
    """

    def __init__(self, f, dialect=csv.excel, encoding="utf-8-sig", **kwds):
        f = UTF8Recoder(f, encoding)
        self.reader = csv.reader(f, dialect=dialect, **kwds)

    def next(self):
        row = self.reader.next()
        return [unicode(s, "utf-8") for s in row]

    def __iter__(self):
        return self

with open('food.csv','rb') as f:
    r = UnicodeReader(f)
    for key,value in r:
        print key,value
ヤクルト 飲み物
カキフライ 洋食
エビフライ 洋食
豚カツ 洋食

相关问题 更多 >