简体   繁体   中英

Python: how to read .csv file in an efficient way?

I have to read some .csv files and do some operations. In particular I have to read .csv where the data is stored in different columns. In particular the data has the following format:

myfile_0.csv

Time InfD  Com ComN
  0   3     4   0
  1   2     5   1

The file contains many entries and I have to do that for different parameters an the process is really slow. In the following the task that I have to accomplish

for i in parameters:
    f = folder+'myfile_%d.csv'%i
    df = pd.read_csv(f)
    D  = df.InfD / V
    C =  (df.Com/df.ComN)  
    size = TC - len(C)
    if len(C) < TC:
        CC = np.lib.pad(C, (0,size), 'constant', constant_values=(1))
        DD = np.lib.pad(D, (0,size), 'constant', constant_values=(0))
        cf = CC*(1-DD)
    else:
        C = C[0:TC]
        D = D[0:TC]
        cf = C*(1-D)

I am wondering if there is a more efficient to solve the same problem.

Try the python csv library

import csv
with open('myfile_0.csv', 'rb') as csvfile:
    reader = csv.reader(csvfile, delimiter=' ', quotechar='|')
    for row in reader:
        print ', '.join(row)

# output:
# Time, InfD, Com, ComN
# 0, 3, 4, 0
# 1, 2, 5, 1

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM