Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
238 views
in Technique[技术] by (71.8m points)

Reading log files in python

I have a log file (it is named data.log) containing data that I would like to read and manipulate.

The file is structured as follows:

'''

#Comment line 1
#Comment line 2 
1.00000000,3.02502604,343260.68655952,384.26845401,-7.70828175,-0.45288215
2.00000000,3.01495320,342124.21684440,767.95286901,-7.71506536,-0.45123853
3.00000000,3.00489957,340989.57100678,1151.05303883,-7.72185550,-0.44959182

'''

I would like to obtain the numbers from the last two columns and convert this into separate arrays or lists, I tried doing this by creating an empty list, but I do not know how to make this from a log file with a certain name. Could someone help me with this as I am a beginner programmer?

The expected output I would like to obtain is:

list1 = [-7.70828175, -7.71506536, -7.71506536] list2 = [-0.45288215, -0.45123853, -0.44959182]

Thank you in advance!

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Try this way. but you have to confirm that each row list length must equal to 6.

list1 = []
list2 = []
with open('example.log') as f:
    for i in f.readlines():
        if (len(i.split(',')) == 6):
            list1.append(i.split(',')[4])
            list2.append(i.split(',')[5])

print(list1)
print(list2)

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...