reading csv file without for

前端 未结 8 1709
孤街浪徒
孤街浪徒 2020-12-10 04:28

I need to read a CSV file in python.

Since for last row I receive a \'NULL byte\' error I would like to avoid using for keyword but the while.

Do you know ho

8条回答
  •  我在风中等你
    2020-12-10 05:05

    If your problem is specific to the last line being empty, you can use numpy.genfromtxt (or the old 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')
    

提交回复
热议问题