Ich möchte .txt-Dateien mit Code automatisch bearbeiten. Alles, was victory_poins enthält, wird entfernt und in einer anderen Form nach der Anweisung "history = {" eingegeben. Aber am Ende fügt es eine zusätzliche Geschichte = {hinzu. Warum?Bearbeiten einer TXT-Datei - Algorithmus funktioniert nicht
Code:
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()
Eingang:
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
}
}
Ausgang:
[...]
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
}
}
Wo kommt die zweite Geschichte = {kommen aus?
Es gibt nur eine 'history = {' Zeile für mich aus. – yper
'" \ thistory "' ist in der Zeile, so dass es die erste Geschichte schreibt. Dann 'vpsegment == False', also gehst du zur else-Anweisung und schreibst die Zeile (die" history {'") enthält – Fabich