2017-07-21 55 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