reading csv files in scipy/numpy in Python

前端 未结 5 1795
轻奢々
轻奢々 2020-12-16 08:23

I am having trouble reading a csv file, delimited by tabs, in python. I use the following function:

def csv2array(filename, skiprows=0, delimiter=\'\\t\', ra         


        
5条回答
  •  忘掉有多难
    2020-12-16 08:33

    I think Nick T's approach would be the better way to go. I would make one change. As I would replace the following code:

    for row, record in enumerate(reader):
    if len(record) != fields:
        print "Skipping malformed record %i, contains %i fields (%i expected)" %
            (record, len(record), fields)
    else:
        records.append(record)
    

    with

    records = np.asrray([row for row in reader if len(row) = fields ])
    print('Number of skipped records: %i'%(len(reader)-len(records)) #note you have to do more than len(reader) as an iterator does not have a length like a list or tuple
    

    The list comprehension will return a numpy array and take advantage of pre-compiled libraries which should speed things up greatly. Also, I would recommend using print() as a function versus print "" as the former is the standard for python3 which is most likely the future and I would use logging over print.

提交回复
热议问题