[英]Python: Elegantly print all *remaining* elements from list in one line
[英]First row from a CSV file is a list of one string while the remaining rows are list of elements in Python
我是新手,正在嘗試使用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模塊執行此操作。
我建議您使用pandas
而不是csv
因為它能夠讀取和創建電子表格。 每個文件的第一行通常用於命名列。 由於每一列都有一個唯一的名稱,因此您無需知道位置,因為在熊貓中,可以按名稱訪問列。
import pandas as pd
data = pd.read_csv(yourFilePath, sep = ';', index_col = 0, parse_dates = True)
這將創建一個類似於MS Excel的電子表格。 第一列“ createTime”將是電子表格的索引,而熊貓會嘗試將此列中的字符串解析為日期/時間信息。
如果您現在想做一些數學運算,則可以逐列進行,因為pandas
能夠正確地解釋這一點。 對於選擇行,您可以按標簽使用選擇:
import pandas as pd
data1 = pd.read_csv(yourFilePath1, sep = ';', index_col = 0, parse_dates = True)
data2 = pd.read_csv(yourFilePath2, sep = ';', index_col = 0, parse_dates = True)
#select a specific time range only, e.g 22th Sept. 2014
range_of_interest = data.loc['20140922']
#select time range >= Sept. 22th, 2014
range_of_interest = data.loc['20140922':]
您還可以根據索引對齊數據框。 這是一個類似的例子
aligned_data = data1.join(data2, how = 'outer', rsuffic = '_2')
還有多種功能可讓您計算列的描述,例如平均值,標准偏差等。例如,“ Grid kW”列的靜態值:
data['Grid kW'].describe()
最后,您可以對完整的數據框進行數學運算。 熊貓會自動使用正確的列。 如果其中一個數據幀中缺少專用列,則計算結果將為“ nan”。 對於缺失值,同樣有效。 因此,數據幀之間的計算結果將始終導致僅具有有效數據的數據幀。
data1.loc['20140922'] + data2.loc['20140922']
那應該涵蓋您的意圖。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.