简体   繁体   中英

Better way for reading 6 CSV files without header

I need a good way for storing all variables from 6 similar csv files without headers. my code look like this now.

I can store as a list if its one CSV, but I have 5 more, What type of variable, I can choose for this problem?

Note: i need only 26 Column from CSV file, which has 257 Columns

import csv

Date = []
Serial_No = []
Range = []
Size = []
RPM = []

#...
#20 more lines like this

CW_Stall_Current_A = []

with open("sample.csv",'r') as csv_file:
    csv_reader=csv.reader(csv_file,delimiter=',')
    for lines in csv_reader:
        Date.append(lines[0])
        Serial_No.append(lines[1])
        Range.append(lines[5])
        Size.append(lines[6])
        RPM.append(lines[13])
        #...    
        #20 more lines like this
        CW_Stall_Current_A.append(lines[147])

You could use a list of index+column name tuples instead of a fixed number of hard-coded variables:

import csv

def extract_csv(filename, columns, delimiter=',', encoding='utf8'):
    """extracts given columns from a CSV file into a dict.
       columns must be a list of (idx, name) tuples"""

    data = {name: [] for idx, name in columns}

    with open(filename, encoding=encoding, newline='') as csv_file:
        reader = csv.reader(csv_file, delimiter=delimiter)
        for row in reader:
            for idx, name in columns:
                data[name].append(row[idx])
    return data

Usage:

sample_data = extract_csv('sample.csv', [
    (0, 'Date'),
    (1, 'Serial_No'),
    (5, 'Range'),
    (6, 'Size'),
    (13, 'RPM'),
    # ... 20 more lines like this
    (147, 'CW_Stall_Current_A'),
])

print(sample_data['Date'])

If working memory size allows, your solution is easy.

Just put your current work inside the indent and specify the filename to open with a variable.

You can list the 6 filenames and call them in order.

The code would look like this:

import csv

Date = []
Serial_No = []
Range = []
Size = []
RPM = []

#...
#20 more lines like this

CW_Stall_Current_A = []

data_files = ['sample.csv','sample2.csv','sample3.csv','sample4.csv','sample5.csv','sample6.csv']

for filename in data_files:
    with open(filename,'r') as csv_file:
        csv_reader=csv.reader(csv_file,delimiter=',')
        for lines in csv_reader:
            Date.append(lines[0])
            Serial_No.append(lines[1])
            Range.append(lines[5])
            Size.append(lines[6])
            RPM.append(lines[13])
            #...    
            #20 more lines like this
            CW_Stall_Current_A.append(lines[147])

## Do something with the whole data

Or, if you want to process each file individually and independently, you can put the initialization of the variable list in a loop.

The code would look like this:

import csv

data_files = ['sample.csv','sample2.csv','sample3.csv','sample4.csv','sample5.csv','sample6.csv']
    
for filename in data_files:  

    Date = []
    Serial_No = []
    Range = []
    Size = []
    RPM = []
    
    #...
    #20 more lines like this
    
    CW_Stall_Current_A = []
    
    with open(filename,'r') as csv_file:
        csv_reader=csv.reader(csv_file,delimiter=',')
        for lines in csv_reader:
            Date.append(lines[0])
            Serial_No.append(lines[1])
            Range.append(lines[5])
            Size.append(lines[6])
            RPM.append(lines[13])
            #...    
            #20 more lines like this
            CW_Stall_Current_A.append(lines[147])
    
    ## Do something with the data in each file

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