0

I have 2 files as shown below

Branch Data:

open bus from 2311 to 3223 ck 1
open bus from 4321 to 3783 ck 1
.
.
.

I have around 100 branches like this

infile:

contingency 'b2_452'
QQQQQQQQ
open bus from 4321 to 3784 ck 1
end
.
.
.

I have around 200 contingencies like this

I am trying to get the output as shown below

excepted output files:

file1

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

Here is my code

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()

I am new to python programming and not sure where I am going wrong. This code is creating two copies of my infile... not replacing QQQQQQQQ with the line.

4

1 回答 1

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()
于 2013-11-21T20:58:31.550 回答