-5

I have this working code:

S095    = 'S095' 
E095    = 'E095'

DocLan = 'c:\DocLan.asc'
tempDocLan = open( DocLan, 'r+' )

for line in fileinput.input( DocLan ):
    if fileinput.lineno() > 1:
        LanNr = find_nth(line, "S095", 1), " - ",
                line[find_nth(line, "S095", 1) + 5: find_nth(line, "S095", 1) + 14]
        if LanNr > 10:
            tempDocLan.write(line.replace(S095, E095))
        else:
            ""
    else:
        tempDocLan.write(line)

tempDocLan.close()

I have tried to change the code to add a second write operation :

for line in fileinput.input(DocLan):
    if fileinput.lineno() > 1 :
        LanNr = find_nth(line, "S095", 1), " - ",
                line[find_nth(line, "S095", 1) + 5: find_nth(line, "S095", 1) + 14]

        if LanNr > 10:
            tempDocLan.write(line.replace(S095, E095))

            # error here
            tempDocLan.write(line[0:50])
        else:
            ""
    else:
        tempDocLan.write(line)

tempDocLan.close()

In the first write I replaced the text S095 for E095 In the second write I want to cut the text (with already E095), but it does not work.


I am using Python 2.7.

I'll give an example. I have a file with this content.

delete, S095/20140001,

delete, S095/20140002,

delete, S095/20140003,

delete, S095/20140004,

delete, S095/20140005,

delete, S095/20140006,

And I want to create this File.

E095/20140001,

E095/20140002,

E095/20140003,

E095/20140004,

E095/20140005,

E095/20140006,

Thanks for help.

Romeu Klug

ChrisF
  • 134,786
  • 31
  • 255
  • 325

1 Answers1

0

It that's all you want to do, it's pretty easy:

  • Split the lines by spaces.
  • Replace S with E.
  • Write.

And it's done.

with open("input_file.txt") as input_file, open("output.txt", "w") as output:
    for line in input_file:
        _, tag = line.split()
        tag.replace("S", "E")

        output.write(tag)
xbello
  • 7,223
  • 3
  • 28
  • 41