Question how to modify a specific string in a text file

Asked 2 years ago, Updated 2 years ago, 16 views

Hello, masters First of all, what I'm curious about is that I need to modify certain strings in a text file, and I wonder if there is a proper default source code that I can use.

The picture above is a capture of a part of the text file, and you have to change the values of each V in the yellow box here.

f = open("/home/sjhyun960/RTN_Boram/testfile.printtr0", 'r')
datafile = f.read()
f.close()

Data_List = datafile.splitlines() #Change to list structure and save each line to index

data_List_Last = data_List[-1] #LastIndex

final_data = data_List_Last.split() #Remove last index tab

Last_transient_simulation = final_data[0] #declare each value variable
transient_time = final_data[1]
Vin = final_data[2]
Vout1 = final_data[3]
Vout2 = final_data[4]
Vout3 = final_data[5]
Vout4 = final_data[6]

This is the output result obtained by executing the above code

Here, for example, in the string 'V(in) = 1' of the first picture, the value of 1 corresponds to the picture directly above

V(in) value of 2.0125e-01 should be modified. Can I modify it additionally and save it automatically?

It was a little long, but I hope you will help me.!

python

2022-09-21 11:18

1 Answers

Pre-Run Files

*.DC VGS 0 1.2 0.01

.IC V(IN)=1
.IC V(OUT1)=0
.IC V(OUT2)=1
.IC V(OUT3)=0
.IC V(OUT4)=1

*.tran 0.1n 70n
.tran 0.1n 5n

Execution code

from parse import compile
f = open('1.txt', 'r+')

_dict = {'IN':'2.01235e-1', \
'OUT1':'9.6849e-01',\
'OUT2':'5.4019e-02',\
'OUT3':'3.2711e-02',\
'OUT4':'1.0327e+00'}

_PATTERN = compile('.IC V({})={}')
_FILELINE = []
for i in f:
    _FILTER = _PATTERN.parse(i)
    if _FILTER is None:
        _FILELINE += [i]
    elif _FILTER[0] in _dict and _FILTER[1] != _dict[_FILTER[0]]:
        _FILELINE += ['.IC V(%s)=%s\n'%(_FILTER[0], _dict[_FILTER[0]])]
    else:
        _FILELINE += [i]


f.seek(0)
f.truncate()
for i in _FILELINE:
    f.write(i)

f.close()

Post-Run Files

*.DC VGS 0 1.2 0.01

.IC V(IN)=2.01235e-1
.IC V(OUT1)=9.6849e-01
.IC V(OUT2)=5.4019e-02
.IC V(OUT3)=3.2711e-02
.IC V(OUT4)=1.0327e+00

*.tran 0.1n 70n
.tran 0.1n 5n

After reading the entire file, use a library called parse to *.ICV(*)=* pattern matched

Add to specific variables by modifying only parts

At the end, the entire file is erased and re-entered. :P

It's not the answer, but please keep that in mind


2022-09-21 11:18

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.