简体   繁体   English

使用Python在CSV文件中对列进行乘法/除法

[英]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. 所以我有很多CSV文件,每个文件中有6列数字。 I would like to perform a few operations (multiplication, division etc.) on each column in each of the CSV files using Python. 我想使用Python对每个CSV文件中的每一列执行一些操作(乘法,除法等)。

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. 所以我使用上面的这个小脚本来校准每一行,这对于每个.csv文件都可以正常工作。 Now all I want to do is to run this script for 200 FILES in one folder. 现在,我想要做的就是在一个文件夹中运行200个FILES的脚本。 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. 假设您知道每列的排列和数据类型,最好的选择是NumPyloadtxt函数。

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. f4i4指的是每列的数据类型 - f4是32位浮点数, i4是32位整数。 Other options are i8 , f8 , or aN for 64 bit integer, 64 bit float, and N length string. 其他选项是i8f8aN用于64位整数,64位浮点数和N长度字符串。

One caveat - if your data contains strings which include commas, the loadtxt function doesn't handle them well. 一个警告 - 如果您的数据包含包含逗号的字符串,则loadtxt函数无法很好地处理它们。 You'll have to use the csv module as recommended by other posters in that case. 在这种情况下,您必须按照其他海报的建议使用csv模块。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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