简体   繁体   中英

Replace Line with New Line in Python

I am reading a text file and searching data line by line, based on some condition, changing some values in the line and writing it back into another file. The new file should not contain the old Line. I have tried the following, but it did not work. I think I am missing a very basic thing.

Solution: In C++ we can increment line but in Python I am not sure how to achieve this. So as of now, I am writing old line than new line. But in the new file, I want only the new line.

Example:

M0 38 A 19 40 DATA2 L=4e-08 W=3e-07 nf=1 m=1 $X=170 $Y=140 $D=8
M0 VBN A 19 40 TEMP2 L=4e-08 W=3e-07 nf=1 m=1 $X=170 $Y=140 $D=8 

The code which i tried is the following:

def parsefile():
    fp = open("File1", "rb+")
    update_file = "File1" + "_update"
    fp_latest = open(update_file, "wb+")  
    for line in fp:
        if line.find("DATA1") == -1:
            fp_latest.write(line)
        if line.find("DATA1") != -1:
            line = line.split()
            pin_name = find_pin_order(line[1])
            update_line = "DATA " + line[1] + " " + pin_name
            fp_latest.write(update_line)
            line = ''.join(line) 
         if line.find("DATA2") != -1:
            line_data = line.split()
            line_data[1] = "TEMP2"
            line_data =' '.join(line_data)
            fp_latest.write(line_data)
         if line.find("DATA3") != -1:
            line_data = line.split()
            line_data[1] = "TEMP3"
            line_data =' '.join(line_data)
            fp_latest.write(line_data)

 fp_latest.close()
 fp.close()

The main problem with your current code is that your first if block, which checks for "DATA1" and writes the line out if it is not found runs when "DATA2" or "DATA3" is present. Since those have their own blocks, the line ends up being duplicated in two different forms.

Here's a minimal modification of your loop that should work:

for line in fp:
    if line.find("DATA1") != -1:
        data = line.split()
        pin_name = find_pin_order(data[1])
        line = "DATA " + data[1] + " " + pin_name
    if line.find("DATA2") != -1:
        data = line.split()
        data[1] = "TEMP2"
        line =' '.join(data)
    if line.find("DATA3") != -1:
        data = line.split()
        data[1] = "TEMP3"
        line =' '.join(data)

    fp_latest.write(line)

This ensures that only one line is written because there's only a single write() call in the code. The special cases simply modify the line that is to be written. I'm not sure I understand the modifications you want to have done in those cases, so there may be more bugs there.

One thing that might help would be to make the second and third if statements into elif statements instead. This would ensure that only one of them would be run (though if you know your file will never have multiple DATA entries on a single line, this may not be necessary).

If you want to write a new line in a file replacing the old content that has been readed last time, you can use the file.seek() method for moving arround the file, there is an example.

with open("myFile.txt", "r+") as f:
    offset = 0
    lines = f.readlines()
    for oldLine in lines:
            ... calculate the new line value ...
            f.seek(offset)
            f.write(newLine)
            offset += len(newLine)
            f.seek(offset)

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