从 Python 中随机目录的文本文件中读取数据
Read data from text file from random directory in Python
我想从具有随机目录的文本文件中读取数据(数字)。文本文件包含看起来像这样的单词和数字,我如何提取这些列。
Start Time: 7/28/2019 7:58:06 PM Time Completed: 7/28/2019 8:21:24 PM Elapsed Time: 00:23:17
Sample ID: 190728-MTJ-IP
***DATA***
Field(Oe) Moment(emu)
987.95878 0.000046470297
963.27719 0.000046452876
938.57541 0.000046659299
913.89473 0.000046416303
889.19093 0.000046813005
864.50576 0.000047033128
839.80973 0.000046368291
815.12703 0.000046888714
790.45031 0.000045933749
765.75385 0.00004716459
741.05444 0.000046405491
我打算用这个但是我很困惑,我应该放什么索引:
def txtread(filepath):
data = []
with open(filepath+'.txt', 'r') as readfile:
datalines = readfile.readlines()
for lines in datalines:
temp = lines.strip('\t\n').split(',')
temp = np.array(temp[:],dtype=float)
data = np.array(data[0::2])
H = data[:,0]
M = data[:,1]
Pandas read_csv 方法有一堆参数来处理所有这些:
>>> import pandas as pd
>>> pd.read_csv('temp.txt', skiprows=5, delim_whitespace=True)
Field(Oe) Moment(emu)
0 987.95878 0.000046
1 963.27719 0.000046
2 938.57541 0.000047
3 913.89473 0.000046
4 889.19093 0.000047
5 864.50576 0.000047
6 839.80973 0.000046
7 815.12703 0.000047
8 790.45031 0.000046
9 765.75385 0.000047
10 741.05444 0.000046
pd.read_csv
的输出是 DataFrame
。如果您更喜欢使用 numpy 数组,
df = pd.read_csv(...)
np_data = df.values
我想从具有随机目录的文本文件中读取数据(数字)。文本文件包含看起来像这样的单词和数字,我如何提取这些列。
Start Time: 7/28/2019 7:58:06 PM Time Completed: 7/28/2019 8:21:24 PM Elapsed Time: 00:23:17
Sample ID: 190728-MTJ-IP
***DATA***
Field(Oe) Moment(emu)
987.95878 0.000046470297
963.27719 0.000046452876
938.57541 0.000046659299
913.89473 0.000046416303
889.19093 0.000046813005
864.50576 0.000047033128
839.80973 0.000046368291
815.12703 0.000046888714
790.45031 0.000045933749
765.75385 0.00004716459
741.05444 0.000046405491
我打算用这个但是我很困惑,我应该放什么索引:
def txtread(filepath):
data = []
with open(filepath+'.txt', 'r') as readfile:
datalines = readfile.readlines()
for lines in datalines:
temp = lines.strip('\t\n').split(',')
temp = np.array(temp[:],dtype=float)
data = np.array(data[0::2])
H = data[:,0]
M = data[:,1]
Pandas read_csv 方法有一堆参数来处理所有这些:
>>> import pandas as pd
>>> pd.read_csv('temp.txt', skiprows=5, delim_whitespace=True)
Field(Oe) Moment(emu)
0 987.95878 0.000046
1 963.27719 0.000046
2 938.57541 0.000047
3 913.89473 0.000046
4 889.19093 0.000047
5 864.50576 0.000047
6 839.80973 0.000046
7 815.12703 0.000047
8 790.45031 0.000046
9 765.75385 0.000047
10 741.05444 0.000046
pd.read_csv
的输出是 DataFrame
。如果您更喜欢使用 numpy 数组,
df = pd.read_csv(...)
np_data = df.values