Skip to content

Instantly share code, notes, and snippets.

@seafooler
Created October 17, 2020 17:19
Show Gist options
  • Save seafooler/ad45411ead791cadbb1b3e8076c4ef2a to your computer and use it in GitHub Desktop.
Save seafooler/ad45411ead791cadbb1b3e8076c4ef2a to your computer and use it in GitHub Desktop.
Parse a experimental log in Python
#!/usr/bin/python3
import csv
data = []
filename = "log-cnn-node1"
# To deal with the lines like [epoch 1, 600 inst] Testing ACC: 0.0883, Loss: 2.3214
with open(filename+".txt", "r") as f:
lines = f.readlines()
i = 0
while i < len(lines):
if "Testing ACC:" in lines[i]:
bd = [None] * 3
sublines = lines[i].split("inst] ")
# deal with: [epoch 11, 600
subline_former = sublines[0]
epoch_num = ((subline_former.split(","))[0]).strip("[epoch ")
bd[0] = epoch_num
# deal with: Testing ACC: 0.72, Loss: 0.83
subsublines = (sublines[1]).split(", ")
acc = (subsublines[0]).strip("Testing ACC: ")
loss = (subsublines[1]).strip("Loss: ").strip("\n\r")
print("epoch: ", epoch_num, ", acc: ", acc, ", loss: ", loss)
bd[1] = acc
bd[2] = loss
data.append(bd)
i=i+1
with open(filename+".csv", "w", newline='') as f:
writer = csv.writer(f)
writer.writerows(data)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment