Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

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

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

1 Answer

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)

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...