2017-07-21 54 views
0

我正在處理一個包含8列的文本數據文件,每個文件列出了溫度,時間,阻尼係數等。我只需要在0.320到0.322的溫度範圍內獲取數據行。 這是我的數據的採樣線(有成千上萬的行):Python中的數據處理

time temp acq. freq. amplitude damping  etc.... 
6.28444 0.32060 413.00000 117.39371 48.65073 286.00159 

我關心的列有時間,溫度,和阻尼。我需要這三個值附加到我的列表中,但只有當溫度在指定的範圍內時(我的數據中有一些行的溫度一直在4開爾文上,而這些數據是垃圾的)。

我使用Python 3.以下是我已經嘗試的事情迄今

f = open('alldata','r') 
c = f.readlines() 
temperature = [] 
newtemp = [] 
damping = [] 
time = [] 

for line in c [0:]: 
line = line.split() 
temperature.append(line[1]) 
damping.append(line[4]) 
time.append(line[0]) 

for i in temperature: 
if float(i)>0.320 and float(i)<0.325: 
    newtemp.append(float(i)) 

當我打印清單newtemp,我看得出來,這個代碼並正確填寫,只有在溫度值列表範圍,但是我也需要我的阻尼列表和時間表,現在只能填充與那個小溫度範圍相對應的值。我不確定如何使用此代碼實現該目標。

我也試過,有人建議在這裏:

output = [] 
lines = open('alldata', 'r') 
for line in lines: 
temp = line.split() 
if float(temp[1]) > 0.320 and float(temp[1]) < 0.322: 
    output.append(line) 
print(output) 

我也得到一個錯誤,指出:

IOPub數據速率超標。 筆記本服務器將暫時停止向客戶端發送輸出 以避免崩潰。 要更改此限制,請設置配置變量 --NotebookApp.iopub_data_rate_limit

我會注意到,我對編碼非常陌生,所以如果事實證明這是一個愚蠢的問題,我很抱歉。

+0

什麼是文件的格式?例如,CSV,TSV還是Excel? – tuomastik

+0

你想使用Python腳本從文件中提取數據嗎?你有沒有嘗試過實現某些東西?你有什麼樣的文件?您需要提供更多信息,以便我們可以爲您提供幫助。請提供一個小例子的數據。 – KelvinS

+0

你有什麼類型的文件? excel,csv,txt?我在python中發佈了一個使用pandas模塊的csv和excel案例。 – sera

回答

1

數據:

temperature, time, coeff... 
0.32, 12:00:23, 2,.. 
0.43, 11:22:23, 3,.. 

在此,溫度是在第一列中。

output = [] 
lines = open('data.file', 'r') 
for line in lines: 
    temp = line.split(',') 
    if float(temp[0]) > 0.320 and float(temp[0]) < 0.322: 
     output.append(line) 
print output 
+0

這是一個很好的例子,但它取決於文件類型和數據分隔符。而且,我認爲'temp'變量應該被轉換爲'float'。 – KelvinS

0

您可以使用熊貓模塊:

import pandas as pd 

# if the file with the data is an excel file use: 
df = pd.read_excel('data.xlsx') 

# if the file is csv 
df = pd.read_csv('data.csv') 

# if the column name of interest is named 'temperature' 
selected = df['temperature'][(df['temperature'] > 0.320) & (df['temperature'] < 0.322)] 

如果你沒有安裝熊貓看到here