简体   繁体   中英

replacing a string in a text file

I wrote a script that will open my text file search for a certain word, then select the line that contains this word ans split it into three parts, then it chooses the part which is a number and add 1 to it, so every time I run the script one is added to this number. here is the script:

#!/usr/bin/env python
inputFile = open('CMakeLists.txt', 'r')

version = None
saved = ""

for line in inputFile:
    if "_PATCH " in line:
        print "inside: ", line
        version = line

    else:
        saved += line

inputFile.close()

inputFile = open('CMakeLists.txt', 'w')

x = version.split('"')
print "x: ", x

a = x[0]
b = int(x[1]) + 1
c = x[2]

new_version = str(a) + '"' + str(b) + '"' + str(c)
print "new_version: ", new_version

inputFile.write(str(saved))
inputFile.write(str(new_version))

inputFile.close()

but my problem is that the new number is being written at the end of the file, I want it to stay in its original place. Any ideas ?

thanks

The problem is that you write the new version number after the original file (without the version line):

inputFile.write(str(saved))
inputFile.write(str(new_version))

You could fix it by saving the lines before and after the line that contains the version separately and then save them in the right order:

#!/usr/bin/env python
inputFile = open('CMakeLists.txt', 'r')

version = None
savedBefore = ""
savedAfter = ""

for line in inputFile:
    if "_PATCH " in line:
        print "inside: ", line
        version = line
    elif version is None:
        savedBefore += line
    else:
        savedAfter += line

inputFile.close()

inputFile = open('CMakeLists.txt', 'w')

x = version.split('"')
print "x: ", x

a = x[0]
b = int(x[1]) + 1
c = x[2]

new_version = str(a) + '"' + str(b) + '"' + str(c)
print "new_version: ", new_version

inputFile.write(savedBefore)
inputFile.write(str(new_version))
inputFile.write(savedAfter)

inputFile.close()

Note: you might need to add some extra text with the version line to make it have the same format as the original (such as adding "_PATCH").

There is a lots to say on your code.

Your mistake is that you're writing your "saved" lines and after you are writing your modified version. Hence, this modified line will be written at the end of the file.

Moreover, I advice you to use with statements.

lines = []    
with open('CmakeLists.txt', 'r') as _fd:
    while True:
        line = _fd.readline()
        if not line:
            break
        if '_PATCH ' in line:
            a, b, c = line.split('"')
            b = int(b) + 1
            line = '{} "{}" {}'.format(a, b, c)
        lines.append(line)
with open('CmakeLists.txt', 'w') as _fd:
    for line in lines:
        _fd.write(line)

This code is untested and may contains some error... also, if your input file is huge, putting every lines in a list can be a bad idea.

#!/usr/bin/env python
inputFile = open('CMakeLists.txt', 'r')

version = None
saved = ""

for line in inputFile:
    if "_PATCH " in line:
        print "inside: ", line
        version = line
        x = version.split('"')
        print "x: ", x

        a = x[0]
        b = int(x[1]) + 1
        c = x[2]

        new_version = str(a) + '"' + str(b) + '"' + str(c)
        saved += new_version
    else:
        saved += line

inputFile.close()

inputFile = open('CMakeLists.txt', 'w')
inputFile.write(str(saved))

inputFile.close()

if a certain line is found, update its content and add to saved , once for loop ends, just write saved to file

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM