Python 编辑.txt文件-算法不起作用
我想用代码自动编辑.txt文件。所有包含victory_poins的内容都将被删除,并在“history={”语句之后以另一种形式输入。但最终,它会添加一个额外的history={。为什么 代码: 输入:Python 编辑.txt文件-算法不起作用,python,algorithm,python-3.x,Python,Algorithm,Python 3.x,我想用代码自动编辑.txt文件。所有包含victory_poins的内容都将被删除,并在“history={”语句之后以另一种形式输入。但最终,它会添加一个额外的history={。为什么 代码: 输入: state={ id=1 name="STATE_1" # Corsica manpower = 322900 state_category = town history={ owner = FRA victory_points = { 3838 1 } build
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={来自哪里?让我们看看当您阅读这行
history{
时会发生什么:
if "\thistory" in line:
data_in.write(line+'\n\t\tvictory_points = { '+str(capital)+' '+str(vp)+' }\n')
该行包含“\thistory”,因此它会写入行(它会写入第一个“history{”)和其他内容
if "\t\tvictory_points" in line:
vppivot=line
vpsegment=True
由于该行不包含“\t\tvictory\u点”,因此不会发生任何操作
vpsegment==False
因此它转到else语句并编写一行“\thistory{”
它只为我输出一行history={
行。“\thistory”
在该行中,因此它编写第一条历史。然后vpsegment==False
则转到else语句并编写该行(其中包含“历史记录{
”)
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)