简体   繁体   English

用Python中的换行替换行

[英]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. 解决方案:在C ++中我们可以增加行但在Python中我不知道如何实现这一点。 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. 当前代码的主要问题是你的第一个if块检查"DATA1" ,如果没有找到则写出行,当"DATA2""DATA3"存在时运行。 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. 这确保只写入一行,因为代码中只有一个write()调用。 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. 可能有用的一件事是将第二个和第三个if语句改为elif语句。 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). 这将确保只运行其中一个(尽管如果您知道您的文件在一行上永远不会有多个DATA条目,则可能没有必要)。

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. 如果你想在一个文件中写一个新行代替上次已经被重新加入的旧内容,你可以使用file.seek()方法来移动文件,有一个例子。

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)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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