簡體   English   中英

從文本文件循環中的特定行中提取

[英]Extracting from specific lines in a text file loop

我有一個看起來像這樣的文件

--------------Time step: 1 ---------------
Accumulated rewards: 1.5
Alpha: 660
Beta: 173
TCP_Friendliness: 1
Fast_Convergence: 1
State: 3
Retries: 0.0
---------------------------------------------------------------
---------------Time step: 2 ---------------
Accumulated rewards: 2.724744871391589
Alpha: 193
Beta: 0
TCP_Friendliness: 0
Fast_Convergence: 0
State: 3
Retries: 0.0
---------------------------------------------------------------
---------------Time step: 3 ---------------
Accumulated rewards: 3.869459113944921

我想將時間步長值提取到 X 數組中,將累積獎勵值提取到 Y 數組中,我不知道該怎么做,因為我有 0 python 經驗,但這是我寫的初始循環跳過示例中未包含的前幾行(亂碼數據)

with open('Tuner_result_1.txt') as f:
    for _ in range(11):
        next(f)
    for line in f:
        x = [line.split()[0]]
        y = [line.split()[1]]

顯然,第二個 for 中的操作是不正確的,我不確定如何以我想要的方式正確閱讀我想要的行。

myfile = open("Tuner_result_1.txt", "rt") # open lorem.txt for reading text
contents = myfile.read()         # read the entire file into a string
myfile.close()                   # close the file
#print(contents)  

import re
X = re.findall("Time step: ([0-9]+)", contents)

Y = re.findall("Accumulated rewards: ([0-9.]+)", contents)
print(X)
print(Y)

Output:

['1', '2', '3']
['1.5','2.724744871391589','3.869459113944921']

您可以應用下一個正則表達式: Time step: (\d+).+?Accumulated rewards: ([\d\.]+)

代碼:

import re

with open("filename") as f:
    X, Y = zip(*re.findall("Time step: (\d+).+?Accumulated rewards: ([\d\.]+)", 
        f.read(), re.MULTILINE | re.DOTALL))

您可以單獨處理每個匹配項:

X = []
Y = []
with open("filename") as f:
    for match in re.finditer("Time step: (\d+).+?Accumulated rewards: ([\d\.]+)", 
        f.read(), re.MULTILINE | re.DOTALL))
        x, y = match.groups()
        # do smth with x and y or add to list
        X.append(x)
        Y.append(y)

您還可以使用字符串切片:

X = []
Y = []
with open("filename") as f:
    s = f.read()
    x_idx = y_idx = 0
    while True:
        x_idx = s.find("Time step: ", y_idx)
        y_idx = s.find("Accumulated rewards: ", x_idx)
        if x_idx >= 0 and y_idx >= 0:
            x = s[x_idx + 11: s.find(" ", x_idx + 11)]
            y = s[y_idx + 21: s.find("\n", y_idx + 21)]
            # do smth with x and y or add to list
            X.append(x)
            Y.append(y)
        else:
            break

暫無
暫無

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

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