繁体   English   中英

如何在循环中修改文本文件中的特定行?

[英]how to modify specific lines in a text file in a loop?

我正在使用python 2.7(OS-centos6)

我有一个文本文件。 例如,它包含以下几行:

0     4.064  16.786   7.016    0
1     5.520  14.733   5.719    0
2     5.904  17.898   5.222    0
3     3.113  18.613  18.453    0
4     3.629  16.760   5.118    0
            :
            :
            :
398   6.369  14.623    6.624    0
399   5.761  18.084    7.212    0
400   2.436  17.021   10.641    0

最后一列最初包含全0。 基本上是旗帜。 我想修改此文本文件,即, 每当特定条件与特定行匹配时,我都希望将最后一列条目设置为1(即,将标志值更改为1)。 例如,行号3,20,250,400满足此条件。 然后,我想使这些特定行的标志值(最后一列条目)为1,而不更改这些行上存在的其他值。 另外,由于我有很多条件,因此我想循环执行此操作。 因此,我必须每次都转到文件顶部(即针对每个条件),并从上到下进行扫描; 只要满足条件,就将特定行的标志标记为1。

重要说明:我正在使用相同的修改文件,然后仅选择标志值不为1的那些行(用于进一步处理)。对于上述循环的每次迭代,我都想读取此修改文件。 简而言之,这意味着我想为一个标准修改文件(即,将标志设置为1)->然后读取修改后的文件->做处理->然后采用下一个标准->为以下条件将标志设置为1此标准->读取修改后的文件->依此类推。

我想补充一点:要满足的标准每次都要考虑两条不同的线。 例如,如果第三行和第398行的第二列条目之间的差小于2.0,则将第398行的标志设置为1。即,差17.898-18.084小于2.0,因此将第398行的标志设置为1

任何帮助将不胜感激。

好的。 首先,您需要打开文件并阅读每一行。

我建议从一个文件逐行读取文件,然后将其写入第二个文件。

with open("original.dat", "r"), open("new.dat", "w") as source, destination:
    for line in source:
        # split on spaces is the default:
        line_no, v1, v2, v3, flag = line.split()
        # just an example, do whatever checks you need to
        should_set_flag = some_computation(v1, v2, v3)
        if should_set_flag: 
            flag = 1
        destination.write("{} {} {} {} {}\n".format(line_no, v1, v2, v3, flag))

也许我不理解您每次进行一次更改时都读取整个文件的要求。 鉴于这些线似乎彼此独立,所以我不确定为什么这样做是完全必要的。

    f=open("filename",'r')
    data=f.readlines()
    f.close()
    #remove file by using os.rm or using subprocess
    i=0
    while i < len(data):
          #do something
          #make changes to data list
    f=open("filename",'w')
    f.write(data)

那可能是唯一的方法。加载数据,删除旧文件,进行更改,写入新文件。

为什么需要将文件写回? 它只有400行,您可以将这些行保留在内存中并一一处理:

def is_criterion_1_fulfilled(row):
    return row[1]<4 # only an example

def process_1(row):
    print row # or do anything else with the line

def filter_and_process(iterator, criterion, process):
    for row in iterator:
        if criterion(row):
            continue
        process(row)
        yield row

def main():
    with open(filename, 'r') as inp:
        dataset = [map(float, line.split()) for line in inp]
    dataset = list(filter_and_process(dataset, is_criterion_1_fulfilled, process_1))
    dataset = list(filter_and_process(dataset, is_criterion_2_fulfilled, process_2))
    ....

if __name__ == '__main__':
    main()
# Imports
import re

# Functions
def check_data(record, records):
    # TODO Implement check operation
    return False

# Read input data
infile = "data.txt"
with open(infile, "r") as f:
    # Make a list of lists
    records = [re.split('\s+',record) for record in f.read().splitlines()]

# Process the data
for i, record in enumerate(records):
    # enumerate so as to refer to ith record if necessary,
    # but lineno anyway available in record[0]
    if check_data(record, records):
        record[4] = '1'


# Write modified data
outfile = "out%s" % infile
with open(outfile, "w") as f:
    for record in records:
        f.write('\t'.join(record)+'\n')

暂无
暂无

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

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