首页 文章

无需读取csv文件

提问于
浏览
9

我需要在python中读取CSV文件 .

因为对于最后一行,我收到一个'NULL byte'错误,我想避免使用for keyword但是while .

你知道怎么做吗?

reader = csv.reader( file )
    for row in reader  # I have an error at this line
          # do whatever with row

我想用for循环替换for循环,以便我可以检查行是否为NULL .

在CSV模块中读取单行的功能是什么?谢谢

谢谢

附:在追溯之下

Traceback (most recent call last):
  File "FetchNeuro_TodayTrades.py", line 189, in 
    for row in reader:
_csv.Error: line contains NULL byte

8 回答

  • 1

    也许您可以捕获CSV阅读器引发的异常 . 像这样的东西:

    filename = "my.csv"
    reader = csv.reader(open(filename))
    try:
        for row in reader:
            print 'Row read with success!', row
    except csv.Error, e:
        sys.exit('file %s, line %d: %s' % (filename, reader.line_num, e))
    

    或者您可以使用 next()

    while True:
        try: 
            print reader.next()
        except csv.Error:
            print "Error"
        except StopIteration:
            print "Iteration End"
            break
    
  • 0

    您需要(始终)确切地说明您收到的错误消息是什么 . 请编辑您的问题 .

    可能这个:

    >>> import csv; csv.reader("\x00").next()
    Traceback (most recent call last):
      File "<stdin>", line 1, in <module>
    _csv.Error: line contains NULL byte
    >>>
    

    csv模块不是8位干净的;见the docs:“”"Also, there are currently some issues regarding ASCII NUL characters."“”

    错误消息本身是错误的:它应该是“NUL”,而不是“NULL”:-(

    如果文件中的最后一行是空的,那么你赢了't get an exception, you' ll只获得 row == [] .

    假设问题是您的文件中有一个或多个NUL,您需要(1)认真对待文件的创建者(2)如果失败,请在(模式)中读取整个文件= "rb"),删除NUL,并将 fixed_text.splitlines() 提供给csv阅读器 .

  • 16

    Django社区已经解决了Python CSV导入问题,因此可能值得searching for CSV import或发布问题 . 此外,您可以在尝试导入之前直接在CSV文件中编辑违规行 .

  • 1

    如果您的问题特定于最后一行为空,您可以使用numpy.genfromtxt(或旧的matplotlib.mlab.csv2rec)

    $: cat >csv_file.txt
    foo,bar,baz
    yes,no,0
    x,y,z
    
    
    
    $:
    $: ipython
    >>> from numpy import genfromtxt
    >>> genfromtxt("csv_file.txt", dtype=None, delimiter=',')
    array([['foo', 'bar', 'baz'],
           ['yes', 'no', '0'],
           ['x', 'y', 'z']], 
          dtype='|S3')
    
  • 0

    您可以在阅读时尝试清理文件:

    def nonull(stream):
        for line in stream:
            yield line.replace('\x00', '')
    
    f = open(filename)
    reader = csv.reader(nonull(f))
    

    当然,假设只是忽略NULL字符对你有用!

  • 0

    不确定你的意思,但你可以随时检查是否存在

    >>> reader = csv.reader("file")
    >>> for r  in reader:
    ...   if r: print r
    ...
    

    如果这不是您想要的,您应该通过显示对您不起作用的示例来更清楚地描述您的问题,包括样本文件格式和您想要的所需输出 .

  • 3

    我没有工作 . 你不能 grab 这个例外 . 您无法测试 if line . 也许你可以直接检查NULL字节,但我不能迅速做到这一点......如果它总是在最后一行,你当然可以跳过它 .

    import csv
    FH = open('data.csv','wb')
    line1 = [97,44,98,44,99,10]
    line2 = [100,44,101,44,102,10]
    for n in line1 + line2:
        FH.write(chr(n))
    FH.write(chr(0))
    FH.close()
    FH = open('data.csv')
    reader = csv.reader(FH)
    for line in reader:
        if '\0' in line:  continue
        if not line:  continue
        print line
    
    $ python script.py 
    ['a', 'b', 'c']
    ['d', 'e', 'f']
    Traceback (most recent call last):
      File "script.py", line 11, in <module>
        for line in reader:
    _csv.Error: line contains NULL byte
    
  • 1

    处理初始 csv 文件并将 Nul '\0' 替换为空白,然后您可以读取它 . 实际代码如下所示:

    data_initial = open(csv_file, "rU")
    reader = csv.reader((line.replace('\0','') for line in data_initial))
    

    这个对我有用 .

    最初的答案在这里:csv-contain null byte

相关问题