简体   繁体   中英

Using Python to multiply/divide columns in CSV files

So I have a number of CSV files with 6 columns of numbers in each file. I would like to perform a few operations (multiplication, division etc.) on each column in each of the CSV files using Python.

import csv

r = csv.reader(open('F:\python\sample.csv','rb'))

w = csv.writer(open('F:\python\sample_calib.csv','wb',buffering=0))

for row in r:
            a = (float(row[0])-0.0376)/-0.0717
            b = (float(row[1])-0.0376)/-0.0717
            c = float(row[2])/1000
            d = float(row[3])/1000
            e = float(row[4])/1000000
            f = float(row[5])/0.001178
            w.writerow([a,b,c,d,e,f])

So I am using this small script above to calibrate each row and this works fine for each .csv file. Now all I want to do is to run this script for 200 FILES in one folder. Can some one tell me how should I edit the script and what modules to add?

您需要熟悉csv模块: http//docs.python.org/library/csv.html

Assuming you know the arrangement and data type of each column, your best bet would be NumPy and the loadtxt function.

Code to use it would look something like this:

import numpy as np
dtype = np.format_parser(['f4', 'f4', 'i4'], ['col1', 'col2', 'col3'], [])
array = np.loadtxt(path_to_file, dtype, delimiter=',')

Then you can perform operations on an entire column like this.

output = array['col1'] + array['col2']

The f4 and i4 refer to the data type of each column - f4 is a 32 bit floating point number, and i4 is a 32 bit integer. Other options are i8 , f8 , or aN for 64 bit integer, 64 bit float, and N length string.

One caveat - if your data contains strings which include commas, the loadtxt function doesn't handle them well. You'll have to use the csv module as recommended by other posters in that case.

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