[英]Python: One file, various data: How to extract multiple measurements from one .txt file? (pandas)
我有一個數據文件,其中包含測量的不同部分(步驟)。 步數通常在 1 到 10 之間。它具有以下結構:
Mainheader of the measurement (contains the test parameters of each step, thus the length is individual for every measurement)
Step 1
Time Displacement Force *-> the separators are tabs in this file*
*blank row*
[s] [mm] [N]
Data
Data
Data
...
*blank row*
Step 2
Time Displacement Force
*blank row*
[s] [mm] [N]
*no data*
*blank row*
Step 3
Time Displacement Force
*blank row*
[s] [mm] [N]
Data
Data
Data
...
*blank row*
Step4
Time Displacement Force
*blank row*
[s] [mm] [N]
*no data*
*blank row*
步數沒有定義,並且可以在不同的測量值之間變化。 我想提取每個步驟的數據並將其保存到數據框中,並在稍后的階段保存到 a.txt 文件(-> Test1_step1.txt,Test1_step2.txt,...)。 我還需要考慮的是,並非每個步驟都包含數據,因此在這種情況下,應該跳過數據幀(例如,步驟 2 和步驟 4 中沒有數據)。
我的想法是with open(...)
和 go 加載文件,每行使用for
語句和 append 單元行之間的所有行 ([s] [mm] [N]) 和空白行一個空列表(在這種情況下為data )。 如果到達步驟的結尾(空行),則列表應轉換為 df(df 還應具有步驟編號的索引),並且程序繼續下一行。 我之所以要使用 pandas 是因為有些值是nan
,我聽說 pandas 可以很好地處理這個問題。 最后,我想要包含每個步驟的數據的單獨數據幀,例如 df_1(包含來自步驟 1 的數據)、df_3(包含來自步驟 3 的數據)等等。
我試圖一步一步地解決這些問題,並使用下面的代碼來隔離至少第一步,但我剛剛得到一個包含我所有數據的 df,沒有任何空行。
data = []
with open(file, 'r') as f:
for line in f:
if '[s]' in line: # Starting point of the data
for line in f: # go through each line
if line in ['\n','\r\n']: # checks if the line is empty
df = pd.DataFrame(data) # should save data to a data frame and continue with the next line
break
else:
data.append(line) # if line is not empty append to data
也許有人可以幫助我解決我的問題。 預先感謝您的幫助: :)
這是一個開始:
data = []
stepnumber = 0
with open(file, 'r') as f:
line = f.readline()
while len(line) > 0: # assuming last line has a newline, it'll be ''
# check for "Step N" line: read step number
if line.startswith("Step"):
stepnumber = int(line[4:].strip())
elif '[s]' in line: # Starting point of the data
line = f.readline().strip()
while not line == "": # go through each non-empty data line
data.append(line)
line = f.readline().strip()
# after that loop, we have an empty data line
# save data to a data frame and continue with the next line
if len(data) > 0:
df = pd.DataFrame(data)
with open(f"Test1_step{stepnumber}.txt", 'w') as ofh:
ofh.write(df)
data = []
# continue to next line
line = f.readline()
這應該為每個包含該步驟的數據框的非空步驟寫出一個文件Test1_step<stepnumber>.txt
。 相反,如果您想稍后進行更多處理並寫入文件,也許您需要將數據幀 append 到列表或其他內容(可能在包含步驟號的元組中)。
您可能需要更多的錯誤檢查來處理您的真實數據。 這個循環確實處理沒有數據的步驟。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.