1
def openFile(fileName):
    try:
       trainFile  = io.open(fileName,"r",encoding = "utf-8")
    except IOError as e:
       print ("File could not be opened: {}".format(e))
    else:
       trainData = csv.DictReader(trainFile)
       print trainData
       return trainData

def computeTFIDF(trainData):
     bodyList = []
     print "Inside computeTFIDF"
     for row in trainData:
        for key, value in row.iteritems():
             print key, unicode(value, "utf-8", "ignore")
     print "Done"
     return

 if __name__ == "__main__":
     print "Main"
     trainData = openFile("../Data/TrainSample.csv")
     print "File Opened"
     computeTFIDF(trainData)

错误:

Traceback (most recent call last):
  File "C:\DebSeal\IUB MS Program\IUB Sem III\Facebook Kaggle Comp\Src\facebookChallenge.py", line 62, in <module>
    computeTFIDF(trainData)
  File "C:\DebSeal\IUB MS Program\IUB Sem III\Facebook Kaggle Comp\Src\facebookChallenge.py", line 42, in computeTFIDF
    for row in trainData:
  File "C:\Python27\lib\csv.py", line 104, in next
    row = self.reader.next()
UnicodeEncodeError: 'ascii' codec can't encode character u'\u201c' in position 215: ordinal not in range(128)

TrainSample.csv: 是一个 4 列的 csv 文件(带标题)。
操作系统:Windows 7 64 位。
使用 Python 2.x

我不知道这里出了什么问题。我说它忽略编码。但仍然会引发同样的错误。

我认为在控件到达编码之前,它会引发错误。

谁能告诉我哪里出错了。

4

2 回答 2

4

Python 2 CSV 模块不处理Unicode 输入。

以二进制模式打开文件,解析为 CSV后解码。这对于 UTF-8 编解码器是安全的,因为换行符、分隔符和引号都编码为 1 个字节。

csv模块文档在示例部分包含一个包装UnicodeReader,它将为您进行解码;它很容易适应班级:DictReader

import csv

class UnicodeDictReader:
    """
    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", **kwds):
        self.encoding = encoding
        self.reader = csv.DictReader(f, dialect=dialect, **kwds)

    def next(self):
        row = self.reader.next()
        return {k: unicode(v, "utf-8") for k, v in row.iteritems()}

    def __iter__(self):
        return self

将此与以二进制模式打开的文件一起使用:

def openFile(fileName):
    try: 
        trainFile  = open(fileName, "rb")
    except IOError as e:
        print "File could not be opened: {}".format(e)
    else:
        return UnicodeDictReader(trainFile)
于 2013-11-02T09:04:26.110 回答
0

我无法对 Martijn 发表评论,在我将这里留给其他人的小升级之后,哪种解决方案对我来说非常有效:

    def next(self):
    row = self.reader.next()
    try:
        d = dict((unicode(k, self.encoding), unicode(v, self.encoding)) for k, v in row.iteritems())
    except TypeError:
        d = row
    return d

一件事是python 2.6及更低版本不支持dict comprahension。另一个,dicts 可以使用不同的类型,而 unicode 函数不能,所以在 null 或 number 的情况下捕获 TypeError 是值得的。让我发疯的另一件事是,当您使用编码打开文件时它不起作用!就让它简单吧open()

于 2019-03-13T12:57:23.603 回答