2017-05-07 111 views
0

我正在处理有关CSV文件的项目。我几乎没有开始学习Python和它的奥秘。我有一段看起来在一个文件夹中的代码,获取所有“.csv”文件(或者在这种情况下为“.txt”,这就是我如何找到所有海量数据)并读出它的数据。我导入CSV与pandas(它有2列,TimeAmplitude)我想绘制两列。我知道情节是怎么样的(数据被绘制在MATLAB中,但我的工作是为python创建一个软件)。我试图用这个link但我不知道如何使它知道我​​是TimeY-axisAmplitude在Python中绘制CSV数据

这是我整个代码到目前为止

import pandas as pd 
import os 
import tkinter as tk 
from tkinter.filedialog import askdirectory 
import matplotlib.pyplot as plt 


print ("Please choose the path to the CSV Files: ") 


root = tk.Tk() 
root.withdraw() 
root.attributes('-topmost', True) 
path = askdirectory() 

print("\nThe chosen folder is: " + path) 

tmp_paths=[] 
data_tables=[] 
data_paths=[] 
file_ext=[] 
file_name=[] 
sep_name=[] 




for root, dirs, files in os.walk(path):  
    for file in files:      
     if file.endswith(".txt"):   
      tmp_paths=os.path.join(root,file) # 
      tables=pd.read_csv(tmp_paths, header=5, sep=' ',converters={"Time":float, "Ampl":float}) 
      data_tables.append(tables)  
      data_paths.append(tmp_paths)  


file_ext=[name.split("\\",1)[1] for name in data_paths] 
file_name=[name.split(".txt",1)[0] for name in file_ext] 
sep_name=[name.split("-",6) for name in file_name] 

plt.plot(data_tables[1],data_tables[0]) 
plt.show() 

PS:当我尝试绘图它给了我:`KeyError:'时间'。

编辑

使用pandas

2.fixed的eCopy错误的情节代码

新的编辑后,我不再获得1.converted的data_tablesfloatKeyErrorTimeAmpl给出另一个错误,指出指数预计为integers而不是strslices,它接受的唯一值是10,任何其他值是out of index

+0

听起来像是你的'表'字典没有关键的'时间'。目录中正在分析的所有文件是否有“时间”列?文件列名是否作为字典键传递给'tables'? – stephenlechner

+0

@stephenlechner'tables'不是'dict',它是'pandas.DataFrame'。 –

+1

是不是'表格'只是循环中的临时变量?我希望你使用'data_tables'中的一个表进行绘图。顺便说一句,你可以通过使用'pathlib.Path'来简化大量的路径处理。例如,这个循环可以替换为'for file in Path()。glob('**/*。txt'):'。 –

回答