【发布时间】:2015-08-15 17:45:45
【问题描述】:
我是一个新手,正在尝试使用 Python 3 执行一些计算并分析来自大量 CSV 文件的一些数据。每个 CSV 文件的数据存储在大约 30000 行中,以分号分隔。由于这些是系统生成的文件,因此第一行的每个文件的参数名称以不同的顺序排列。所以我想提取这些参数名称,比较它们并找出每个文件所需参数值的位置。
但在我这样做之前,当我读取 CSV 文件的第一行时,列表有一个由“;”分隔的字符串而其余的行被正确提取。我尝试过拆分文本和其他方式,但它们都对输出没有任何影响。有人可以帮助解决这个问题。
这是我写的一段代码:
import os
import sys
import csv
import math
# Open a file
full_path = "C:\\Documents and Settings\\My Documents\\CSV files"
dirs = os.listdir( full_path )
filename = list()
"""This will find the file in the directory and print the first row of the
CSV file - the names of the parameters."""
for file in dirs:
path = full_path+'\\'+file
with open(path, 'rt') as csvfile:
#For printing the location name
Turbine_name = (os.path.splitext(file)[0])
#Reading the files into an object
csvreader = csv.reader(csvfile, delimiter=';')
#Obtaining the first line of the CSV file - the names of the parameters
Parameters = next(csvreader)
print (Parameters)
#From this line, the values of different Parameters are extracted
for row in csvreader:
Parameters = next(csvreader)
print (Parameters)
#To eliminate rows with insufficient data
if len(Parameters)>11:
Created_time = Parameters[0]
Grid_frequency = float(Parameters[3])
Grid_KW = float(Parameters[4])
Rotor_RPM = float(Parameters[10])
这里以参数的位置为例,因为我还没有编写用于比较字符串的代码。输入文件的示例如下所示:
createTime;"Grid CosPhi";"Grid Current";"Grid Frequency";"Grid kW";"Grid VAr";"Grid Voltage";"Pitch angle 1";"Pitch angle 2";"Pitch angle 3";"Rotor RPM";"Temp. 5 214";"Temp. 6 217";"Temp. 9 227";"Winddirection";"Windspeed"
9/21/14 11:30:01 PM;N/A;N/A;49.963;211688.734;N/A;N/A;-1.06;-1.039;-1.119;19.379;47.167;36;64;N/A;6.319
9/21/14 11:40:01 PM;N/A;N/A;50.002;170096.297;N/A;N/A;-1.003;-0.96;-1.058;19.446;47.182;36.084;63.772;N/A;5.628
9/21/14 11:50:01 PM;N/A;N/A;50.021;175038.734;N/A;N/A;-0.976;-0.961;-1.082;18.805;47;36.223;63.153;N/A;5.577
9/22/14 12:00:01 AM;N/A;N/A;49.964;229942.016;N/A;N/A;-1.047;-1.018;-1.066;18.957;47.125;36.293;63.766;N/A;6.431
9/22/14 12:10:01 AM;N/A;N/A;49.908;200873.844;N/A;N/A;-0.997;-0.985;-1.06;19.229;47.028;36.334;63.962;N/A;6.076
9/22/14 12:20:01 AM;N/A;N/A;49.934;234467.609;N/A;N/A;-1.028;-0.986;-1.001;18.995;47.056;36.401;63.732;N/A;6.067
9/22/14 12:30:01 AM;N/A;N/A;49.96;270969.25;N/A;N/A;-1.138;-1.103;-1.122;18.983;47.274;36.499;64.014;N/A;6.487
9/23/14 12:30:01 PM;N/A;N/A;50.073;54458.719;N/A;N/A;-0.492;-0.52;-0.472;13;42.02;33.624;46.426;N/A;3.757
当我得到输出时,它看起来像这样:
>>>['createTime;"Grid CosPhi";"Grid Current";"Grid Frequency";"Grid kW";"Grid VAr";"Grid Voltage";"Pitch angle 1";"Pitch angle 2";"Pitch angle 3";"Rotor RPM";"Temp. 5 214";"Temp. 6 217";"Temp. 9 227";"Winddirection";"Windspeed"']
['9/21/14 11:40:01 PM', 'N/A', 'N/A', '50.002', '170096.297', 'N/A', 'N/A', '-1.003', '-0.96', '-1.058', '19.446', '47.182', '36.084', '63.772', 'N/A', '5.628']
['9/22/14 12:00:01 AM', 'N/A', 'N/A', '49.964', '229942.016', 'N/A', 'N/A', '-1.047', '-1.018', '-1.066', '18.957', '47.125', '36.293', '63.766', 'N/A', '6.431']
['9/22/14 12:20:01 AM', 'N/A', 'N/A', '49.934', '234467.609', 'N/A', 'N/A', '-1.028', '-0.986', '-1.001', '18.995', '47.056', '36.401', '63.732', 'N/A', '6.067']
正如在某些文件中所观察到的,某些参数完全丢失。这就是为什么我需要找出每个 CSV 文件中各个参数的位置。任何如何才能做到最好的想法也值得赞赏。提前致谢。
编辑:不幸的是,我不能使用 Pandas,因为它必须单独安装在 Python34 中,而且我的办公系统(Windows XP、P4)不支持它。如果可能的话,我想用 CSV 模块来做这件事。
【问题讨论】: