The simulation data file saved in .txt format should be retrieved and the last simulated data should be retrieved via Python.
f = open("/home/sjhyun960/desktop/4.txt", 'r') #Bring and read text file
textfile = f.read()
f.close()
After reading the file, I replaced the simple string with a list structure.
TextList = textfile.splitlines() #Change to list structure
print("\n")
After that, I have to find the last data in the text file below using the find and repeat statements properly, but it is blocked here. As I read the entire string, it is also a problem to extract each data.
Is there a default source code available??
I think it's a tab-separated tsv file. Do not parse csv and tsv files by reading text files with difficulty.
Use pandas read_csv
.
>>> import pandas as pd
>>> from io import StringIO
>>> t = '''10 3.3 2.2 7.7
11 33e7 2.1e3 1.333e-33
12 9.12e3 3.32e-11 1.111'''
>>> df = pd.read_csv(StringIO(t), sep='\t', header=None)
>>> df
0 1 2 3
0 10 3.3 2.200000e+00 7.700000e+00
1 11 330000000.0 2.100000e+03 1.333000e-33
2 12 9120.0 3.320000e-11 1.111000e+00
>>> df.iloc[-1]
0 1.200000e+01
1 9.120000e+03
2 3.320000e-11
3 1.111000e+00
Name: 2, dtype: float64
>>> a, b, c, d = df.iloc[-1]
>>> a
12.0
>>> b
9120.0
>>> c
3.3200000000000006e-11
>>> d
1.111
>>>
Map to class using namedtuple.
line = "1111 2222 3333 4444"
from collections import namedtuple
myclass = namedtuple('myclass', ['field1', 'field2', 'field3', 'field4'])
myclass(*line.split())
=> myclass(field1='1111', field2='2222', field3='3333', field4='4444')
© 2025 OneMinuteCode. All rights reserved.