我正在使用Tkinter
Python中的模块制作GUI 。当使用“浏览”按钮打开时,我试图以方便的格式从制表符分隔的文件中获取数据。我现在有以下代码,它将我的数据保存在一个长字符串中(使用时称为data.get()
),其中包括\t
's和\n
's。有没有一种便捷的方法可以将我的数据立即放入pandas
数据框中,而不是过滤出空行,查找标题,在\t
的s等位置分隔?
from Tkinter import *
import tkFileDialog
def browse_file():
file = tkFileDialog.askopenfile()
path.set(file.name)
if file:
data.set(file.read())
root = Tk()
path = StringVar()
data = StringVar()
Entry(root, textvariable=path, width=50).pack(side=LEFT)
Button(root, text="Browse", command=browse_file).pack(side=LEFT)
root.mainloop()
编辑:
Average readings / Time between readings = 1 / 0.10
Rel Time X1 Y1(Volts)
0.812 0.000000E+0 3.652836E-5
1.130 1.000000E-5 8.870999E-3
1.435 2.000000E-5 1.785118E-2
如果您的数据如下所示:
data = """
Average readings / Time between readings = 1 / 0.10
Rel_Time\tX1\tY1(Volts)
0.812\t0.000000E+0\t3.652836E-5
1.130\t1.000000E-5\t8.870999E-3
1.435\t2.000000E-5\t1.785118E-2
"""
那么您可以这样做:
import pandas as pd
import numpy as np
import numpy.ma as ma
import io
data = """
Average readings / Time between readings = 1 / 0.10
Rel_Time\tX1\tY1(Volts)
0.812\t0.000000E+0\t3.652836E-5
1.130\t1.000000E-5\t8.870999E-3
1.435\t2.000000E-5\t1.785118E-2
"""
df = pd.read_table(
io.StringIO(data),
header = 1,
index_col = 0,
dtype = np.float64
)
print(df)
--output:--
X1 Y1(Volts)
Rel_Time
0.812 0.00000 0.000037
1.130 0.00001 0.008871
1.435 0.00002 0.01785
如果这是您想要的事情,那么您需要弄清楚如何将实际数据转换为该格式。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句