I am using a python command to replace some string values in a txt file. Even though the 'print()' command brings the expected changes, they are not implemented in the original file.
Here is the command I am using:
inputFile = open(r'C:\Users\nigel\OneDrive\Documents\LAB\lean\03.33552_R00.txt', 'r')
lines = inputFile.readlines()
inputFile.close()
exportFile = open(r'C:\Users\nigel\OneDrive\Documents\LAB\lean\03.33552_R00.txt', 'w')
list_a=[]
list_b=[]
for line in lines:
if 'PVObject_=pvSubArray' in line:
list_a.append(len(list_a) 1)
if 'PVObject_IAM=pvIAM' in line:
list_b.append(len(list_b) 1)
list_a = [str(x) for x in list_a]
list_b = [str(x) for x in list_b]
print(list_a)
print(list_b)
control_a=0
control_b=0
for line in lines:
if 'PVObject' in line:
if 'PVObject_=pvSubArray' in line:
new_value_a = list_a[control_a]
control_a =1
line = line.replace('=', new_value_a)
line = line.strip()
print(line)
elif 'PVObject_IAM=pvIAM' in line:
new_value_b = list_b[control_b]
control_b =1
line = line.replace('=', new_value_b)
line = line.strip()
print(line)
else:
line = line.replace('=', '')
line = line.strip()
print(line)
exportFile.writelines(lines[0:])
exportFile.close()
Here is the output:
['1', '2', '3', '4']
['1', '2', '3', '4']
PVObject_SiteSimulpvSite
PVObject_MeteoSimulpvMeteo
PVObject_SiteMetpvSite
PVObject_EnIncidentepvEnIncid
PVObject_PlanCapteurspvCollPlane
PVObject_PVMainArraypvMainArray
PVObject_1pvSubArray
PVObject_IAM1pvIAM
PVObject_2pvSubArray
PVObject_IAM2pvIAM
PVObject_3pvSubArray
PVObject_IAM3pvIAM
PVObject_4pvSubArray
PVObject_IAM4pvIAM
PVObject_SystempvSystem
PVObject_OmbragepvShading
PVObject_1pvShdArrayTable
PVObject_2pvShdArrayTable
PVObject_3pvShdGenObject
PVObject_4pvShdGenObject
PVObject_5pvShdGenObject
PVObject_6pvShdGenObject
PVObject_pvShdObjectGroup
PVObject_TableOmbrLin#1pvShdFTable
PVObject_ModuleLayoutpvModuleLayout
SubFields,listof2PVObjects
PVObject_1pvSubField
PVObject_2pvSubField
PVObject_pvSimulPlot
PVObject_pvSimulPlot
PVObject_pvSimulPlot
PVObject_pvSimulPlot
PVObject_pvSimulPlot
PVObject_pvSimulPlot
PVObject_pvSimulPlot
PVObject_pvSimulPlot
PVObject_pvSimulPlot
PVObject_pvSimulPlot
PVObject_pvSimulPlot
PVObject_pvSimulPlot
PVObject_pvSimulPlot
PVObject_pvSimulPlot
As you can see from the print() output, the code does implement changes. However, nothing's changed in file '03.33552_R00.txt'. I suspect there may be some issues with the code after the last print() command:
exportFile.writelines(lines[0:])
exportFile.close()
I appreciate any help!
CodePudding user response:
lines = ['aaaa','bbb','cccc']
for line in lines:
line ='xxxx'
print(line) #output: xxxx
print(lines) #output: ['aaaa', 'bbb', 'cccc']
you do not change the list lines
you write into exportFile
. you change line
= a variable inside the loop that means nothing.
you can solve this problem with something like this:
for e, _ in enumerate(lines):
lines[e] ='xxxx'
print(lines)
#output: ['xxxx', 'bbb', 'cccc']
# ['xxxx', 'xxxx', 'cccc']
# ['xxxx', 'xxxx', 'xxxx']
CodePudding user response:
I appreciate all the suggestions. Here is the final answer:
(...)
control_a=0
control_b=0
for i, line in enumerate(lines):
if 'PVObject' in line:
if 'PVObject_=pvSubArray' in line:
new_value_a=list_a[control_a]
control_a =1
lines[i]=lines[i].replace('=', new_value_a)
elif 'PVObject_IAM=pvIAM' in line:
new_value_b=list_b[control_b]
control_b =1
lines[i]=lines[i].replace('=', new_value_b)
else:
lines[i]=lines[i].replace('=', '')
exportFile.writelines(lines[0:])
exportFile.close()