1
我想用代碼自動編輯.txt文件。包含victory_poins的所有內容都將被刪除,並在「history = {」語句後以另一種形式輸入。但最後,它增加了一個額外的歷史= {。爲什麼?編輯.txt文件 - 算法不工作
代碼:
def überschreiben(filename,vp, capital):
data_out=open(filename,"r")
data_in=open(filename+"_output.txt","w")
vpsegment=False
for line in data_out:
if "\thistory" in line:
data_in.write(line+'\n\t\tvictory_points = { '+str(capital)+' '+str(vp)+' }\n')
if "\t\tvictory_points" in line:
vppivot=line
vpsegment=True
if vpsegment==True:
if "}" in line:
data_in.write("")
vpsegment=False
else:
data_in.write("")
else:
data_in.write(line)
data_in.close()
data_out.close()
輸入:
state={
id=1
name="STATE_1" # Corsica
manpower = 322900
state_category = town
history={
owner = FRA
victory_points = { 3838 1 }
buildings = {
infrastructure = 4
industrial_complex = 1
air_base = 1
3838 = {
naval_base = 3
}
}
add_core_of = FRA
}
provinces={
3838 9851 11804
}
}
輸出:
[...]
state_category = town
history={
victory_points = { 00001 8 }
history={
owner = FRA
buildings = {
infrastructure = 4
industrial_complex = 1
air_base = 1
3838 = {
naval_base = 3
}
}
add_core_of = FRA
}
provinces={
3838 9851 11804
}
}
哪裏第二歷史= {從何而來?
它只輸出一個'history = {'line for me。 – yper
'「\ thistory」'是在行中,所以它寫了第一個歷史。然後'vpsegment == False',所以你去else語句並且寫下這行(它包含「'history {'」) – Fabich