Python 如何在循环中修改文本文件中的特定行?
我使用的是Python2.7OS-centos6 我有一个文本文件。例如,它由以下几行组成:Python 如何在循环中修改文本文件中的特定行?,python,Python,我使用的是Python2.7OS-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 : :
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,20250400符合该标准。然后我想使这些特定行的最后一列条目的标志值为1,而不改变这些行上的其他值。
另外,我想在循环中这样做,因为我有很多标准。因此,我每次都必须到文件的顶部,即每个标准,并从上到下扫描它;只要满足标准,就将特定行的标志标记为1
重要提示:我使用同一个修改过的文件,然后只选择那些标志值不是1的行进行进一步处理。对于上面提到的循环的每次迭代,我都想读取这个修改过的文件。这意味着,简而言之,我想修改文件,即一个标准设置标志为1->然后读取修改的文件->进行处理->然后采用下一个标准->将该标准设置标志为1->读取修改的文件->等等
我想补充一点:
要满足的标准每次考虑两条不同的线。
e、 g.如果第3行和第398行的第2列条目之间的差异小于2.0,则将第398行的标志设置为1。i、 e.差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()
我认为您需要数据库来完成这项工作。使用for循环遍历文件行,并使用modified标志将同一行写入新文件或同一文件。您肯定不需要数据库,尽管它可能更有意义。@迈克:我每次都需要读取/扫描整个文件,因为:假设第一个条件是行号1,5,13200350的标志设置为1。所以其他行的标志是0。我的条件是,我必须从一行开始,每一个下一个条件都有标志0。
# 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')