簡體   English   中英

Python 文件 readline() 方法跳過我的源文件的特定行?

[英]Python File readline() method skipping specific lines of my source file?

對於我正在處理的圖像分類項目,我將報告生成為文本文件。 對於每次訓練驗證運行,應用程序都會在報告文件中寫入一個標題,每行幾行總結使用的 model 的一層,最后是一個結果行,說明准確度、損失和所用的 epoch 數。 以下是報告文件的說明:

>>>test 1<<<
input   convolution channels: 8 activation: relu    l1 regularization: 0.0
hidden  maxpool pool size: 2
hidden  convolution channels: 64    activation: relu    l1 regularization: 0.0
hidden  maxpool pool size: 2
hidden  convolution channels: 64    activation: relu    l1 regularization: 0.0
hidden  flatten 
hidden  dense   neurons: 64 activation: relu    l1 regularization: 0.0
output  dense   neurons: 10 activation: softmax l1 regularization: 0.0
Best validation at epoch 10 with loss 0.65450 and  accuracy 0.96423

>>>test 2<<<
input   convolution channels: 16    activation: relu    l1 regularization: 0.0
hidden  maxpool pool size: 2
hidden  convolution channels: 64    activation: relu    l1 regularization: 0.0
hidden  maxpool pool size: 2
hidden  convolution channels: 64    activation: relu    l1 regularization: 0.0
hidden  flatten 
hidden  dense   neurons: 64 activation: relu    l1 regularization: 0.0
output  dense   neurons: 10 activation: softmax l1 regularization: 0.0
Best validation at epoch 12 with loss 0.47489 and  accuracy 0.97593

現在我需要通過該報告文件 go 並找到測試 model 給我最好的結果所以我做了一個 function 使用搜索readline()導航到驗證的 [行...] ”在之前的報告描述中)。 我希望恢復該行並在其上使用str.split()可以讓我將數據嵌入到這些結果行中,但運行崩潰了,因為我得到的行不是我要查找的行,而是那個行在它之后。 看不出我為什么會在我的代碼中跳過那一行,我嘗試只打印 22 行readline().split()並看看我會得到什么(22 行,因為這對於文件中的前 2 個報告語句來說已經足夠了) . 這是我運行的調試代碼:

ioData = open(REPORT_FILE, 'r')
for i in range(22):
   print("{}".format(ioData.readline().split()))
ioData.close()

而我從中得到的 output :

['>>>test', '1<<<']
['input', 'convolution', 'channels:', '8', 'activation:', 'relu', 'l1', 'regularization:', '0.0']
['hidden', 'maxpool', 'pool', 'size:', '2']
['hidden', 'convolution', 'channels:', '64', 'activation:', 'relu', 'l1', 'regularization:', '0.0']
['hidden', 'maxpool', 'pool', 'size:', '2']
['hidden', 'convolution', 'channels:', '64', 'activation:', 'relu', 'l1', 'regularization:', '0.0']
['hidden', 'flatten']
['hidden', 'dense', 'neurons:', '64', 'activation:', 'relu', 'l1', 'regularization:', '0.0']
['output', 'dense', 'neurons:', '10', 'activation:', 'softmax', 'l1', 'regularization:', '0.0']
[]
['>>>test', '2<<<']
['input', 'convolution', 'channels:', '16', 'activation:', 'relu', 'l1', 'regularization:', '0.0']
['hidden', 'maxpool', 'pool', 'size:', '2']
['hidden', 'convolution', 'channels:', '64', 'activation:', 'relu', 'l1', 'regularization:', '0.0']
['hidden', 'maxpool', 'pool', 'size:', '2']
['hidden', 'convolution', 'channels:', '64', 'activation:', 'relu', 'l1', 'regularization:', '0.0']
['hidden', 'flatten']
['hidden', 'dense', 'neurons:', '64', 'activation:', 'relu', 'l1', 'regularization:', '0.0']
['output', 'dense', 'neurons:', '10', 'activation:', 'softmax', 'l1', 'regularization:', '0.0']
[]
['>>>test', '3<<<']
['input', 'convolution', 'channels:', '32', 'activation:', 'relu', 'l1', 'regularization:', '0.0']

如您所見,打印輸出顯示單詞列表中的每一行以正確的順序拆分,但它跳過了結果行(每個報告語句的最后一行)。 結果,我沒有在 22 行中只得到兩個報告語句,而是到了第三個報告語句的前兩行。

我沒有看到任何可以解釋為什么每次都會跳過特定行的錯誤。 誰能幫我?

哦,我剛剛想通了。 我有一個同名的重復文件(必須在調試報告模塊時生成它)並且該文件與我的報告完全相同,只是其中缺少測試結果行。 一直以來,我都在索引該文件而不是帶有結果的文件。

很抱歉浪費您的時間。

暫無
暫無

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

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