簡體   English   中英

使用 python 讀取文本文件數據

[英]Read text file data using python

我有一個包含許多列和行的文本文件,具有多種數據類型。 我想通過選擇列來讀取 python 和 plot 中的文件值。 我的文件如下所示:

    time        column1        column2        column3        column4        column5        column6        column7 
 100.035   6.667252E+00  -4.106210E+00  -1.577542E-02   4.090584E+00  -3.699584E-01  -6.998253E-02  -6.699544E-01 
 100.075   6.776713E+00  -4.347899E+00  -1.791951E-02   4.329726E+00  -3.699584E-01  -6.998253E-02  -6.699544E-01 
 100.115   6.806808E+00  -4.451121E+00  -1.886022E-02   4.432934E+00  -3.699584E-01  -6.998253E-02  -6.699544E-01 
 100.155   6.826516E+00  -4.534202E+00  -1.924360E-02   4.513488E+00  -3.699584E-01  -6.998253E-02  -6.699544E-01 
 100.195   6.890967E+00  -4.962194E+00  -1.946191E-02   4.943943E+00  -3.699584E-01  -6.998253E-02  -6.699544E-01 
 100.235   6.961544E+00  -5.430468E+00  -1.924892E-02   5.409640E+00  -3.699584E-01  -6.998253E-02  -6.699544E-01 

我嘗試閱讀這里這里提到的文件,還嘗試了一些基於模式的分隔代碼,如這里 到目前為止,下面代碼中的 output 的所有列都在first_columns處被限制為列出的values

import csv
with open ('mps50.txt', 'r') as f:
     first_column = [row[0] for row in csv.reader(f,delimiter='\t')]

但是first_column是一個列表,我想不出如何進一步使用它來幫助我 plot 值。 你能指導我如何去做嗎? 一些示例或鏈接會有所幫助。 first_column 看起來像這樣。

使用pandas

  • 使用pandas.read_csv讀取數據
    • 這假定數據如圖所示,在txt文件中,以空格作為分隔符。
  • 使用matplotlib到 plot
import pandas as pd
import matplotlib.pyplot as plt

df = pd.read_csv('test.txt', sep='\\s+')

# df view
    time   column1   column2   column3   column4   column5   column6   column7
 100.035  6.667252 -4.106210 -0.015775  4.090584 -0.369958 -0.069983 -0.669954
 100.075  6.776713 -4.347899 -0.017920  4.329726 -0.369958 -0.069983 -0.669954
 100.115  6.806808 -4.451121 -0.018860  4.432934 -0.369958 -0.069983 -0.669954
 100.155  6.826516 -4.534202 -0.019244  4.513488 -0.369958 -0.069983 -0.669954
 100.195  6.890967 -4.962194 -0.019462  4.943943 -0.369958 -0.069983 -0.669954
 100.235  6.961544 -5.430468 -0.019249  5.409640 -0.369958 -0.069983 -0.669954

plot 數據:

  • 有許多用於繪制數據的選項。
    • 下面是幾個簡單的例子
# all columns
plt.plot(df['time'], df.iloc[:, 1:], marker='o')
plt.xticks(rotation=90)
plt.show()

在此處輸入圖像描述

# specific column
plt.plot(df['time'], df['column1'], marker='o')
plt.xticks(rotation=90)
plt.show()

在此處輸入圖像描述

  • seaborn
import seaborn as sns

# set the index
df_ind = df.set_index('time')

sns.lineplot(data=df_ind, dashes=False, markers=True)
plt.legend(loc='center left', bbox_to_anchor=(1, 0.5))
plt.xticks(rotation=90)
plt.show()

在此處輸入圖像描述

使用 PyParsing

只是為了給你一個替代方案,以防你的元素分離不均勻,但在這種情況下,我會 go 和 pandas 。

import pyparsing as pp
import matplotlib.pyplot as plt

ifile = open('test.csv','r')
csv_file = ifile.read()
ifile.close()

EOL = pp.LineEnd().suppress()
number = pp.pyparsing_common.number

ncols = 8

row = ( number*ncols + EOL)

results = []

for t, s, e in row.scanString(csv_file):
    results.append(t.asList())

print(results)


[[100.035, 6.667252, -4.10621, -0.01577542, 4.090584, -0.3699584, -0.06998253, -0.6699544],
 [100.075, 6.776713, -4.347899, -0.01791951, 4.329726, -0.3699584, -0.06998253, -0.6699544],
 [100.115, 6.806808, -4.451121, -0.01886022, 4.432934, -0.3699584, -0.06998253, -0.6699544],
 [100.155, 6.826516, -4.534202, -0.0192436, 4.513488, -0.3699584, -0.06998253, -0.6699544],
 [100.195, 6.890967, -4.962194, -0.01946191, 4.943943, -0.3699584, -0.06998253, -0.6699544],
 [100.235, 6.961544, -5.430468, -0.01924892, 5.40964, -0.3699584, -0.06998253, -0.6699544]]

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM