Reading log files in python

2024/7/6 22:12:41

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!

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)
https://en.xdnf.cn/q/120185.html

Related Q&A

Return more than one value in python function [duplicate]

This question already has answers here:How can I use `return` to get back multiple values from a loop? Can I put them in a list?(2 answers)Closed 1 year ago.I was trying to use *args with a for loop …

Python: Making a class to use complex numbers

I am currently trying to write a program that deals with complex numbers. I have to use classes and methods. I am trying to be able to add, subtract, multiply etc., complex numbers, as well as compare …

Return does not return anything in Spyder. It works fine with other IDE

I just moved to spyder for Python and the return function doesnt seem to work: def test():return 2 test()The IPython console is empty. If I use print instead of return it works fine. Any idea? I use p…

Error in goto module [Python]

Ok, let me start by saying I know that it is bad that I am using the goto module and I shouldnt be and blah blah blah. However, for this specific purpose I need it. Let me also say that I am new to Pyt…

How to scrape all product review from lazada in python

i currently working on web scraping of data from the lazada site using selenium in python: https://www.lazada.sg/products/loreal-paris-uv-perfect-even-complexion-sunscreen-spf50pa-30ml-i214861100-s325…

How to compare 2 successive row values in a resultset object using python

I have a table issue_logs:id | issue_id | from_status | to_status | up_date | remarks ----+----------+-------------+-----------+----------------------------------+----------…

Getting all possible combination for [1,0] with length 3 [0,0,0] to [1,1,1]

from itertools import combinationsdef n_length_combo(arr, n):# using set to deal# with duplicates return list(combinations(arr, n))# Driver Function if __name__ == "__main__":arr = 01n = 3pri…

Compare values under multiple conditions of one column in Python

I have the following data:data = {"index": [1, 2, 3, 4, 5],"name": ["A", "A", "B", "B", "B"],"type": [s1, s2, s1, s2, s3]…

Python: Tkinter :Dynamically Create Label

I am trying to create Label Dynamically , I am getting invalid Syntax. Can you please help me what i am missing or any alternativecrsr = cnxn.execute(query)row_num=2column_num=0Variable_Number=1for row…

TypeError: str object is not callable when trying to click datepicker

The relevant HTML<div id="datepickerbox" class="ym-gbox-left"><div class="datepick_label"><div id="datepicker" class="hasDatepicker">…