我有2个文件,如下所示
分行数据:
open bus from 2311 to 3223 ck 1
open bus from 4321 to 3783 ck 1
.
.
.
我有大约100个像这样的分支
infile中:
contingency 'b2_452'
QQQQQQQQ
open bus from 4321 to 3784 ck 1
end
.
.
.
我有大概200个这样的突发事件
我正在尝试获取如下所示的输出
例外输出文件:
文件1
contingency 'b2_452'
open bus from 2311 to 3223 ck 1
open bus from 4321 to 3784 ck 1
end
file2的
contingency 'b2_452'
open bus from 4321 to 3783 ck 1
open bus from 4321 to 3784 ck 1
end
这是我的代码
infile = open('Contingency.txt').read()
Brachdata = open('BranchData.txt', 'r')
i = 0
for branchline in Brachdata:
replace1 = branchline
i = i + 1
outputfile = open('file' + str(i) +'.txt', 'w')
for line in infile:
outputfile.write(line.replace('QQQQQQQQ', replace1))
outputfile.close()
我是python编程的新手,不知道我哪里出错了。这段代码创建了我的infile的两个副本......没有用行代替QQQQQQQQ。
答案 0 :(得分:0)
我弄清楚问题是什么..这里是更新后的代码
f2 = open('Contingency.txt', 'r')
f1 = open('Contingency new.txt', 'w')
for line in f2:
if 'QQQQQQQQ' in line:
f1.write(" @")
else:
f1.write(line)
f1.close()
Brachdata = open('BranchData.txt', 'r')
infile = open('Contingency new.txt').read()
i = 0
for branchline in Brachdata:
replace1 = branchline
i = i + 1
outputFile = open('Contingency' + str(i) +'.txt', 'w')
for line in infile:
if '@' in line:
outputFile.write(replace1)
else:
outputFile.write(line)
Brachdata.close()