在查找关键字时在文件中写一行(不重复)

时间:2017-02-08 20:39:31

标签: python

我正在尝试创建一个函数,当它在文件中找到一些文本时会写一行。示例:

假设我有“my_file.txt”:

hello
friend
hello
brian
hello
kashmir  
Donald

我需要“my_file.txt”:

hello
friend 
hello
friend
brian
hello
kashmir
Donald

我需要,如果再次应用脚本,输入和输出是相同的,它是:

hello
friend 
hello
friend
brian
hello
kashmir
Donald

第一个问题是我希望它写“朋友”而不是第一次发现“你好”而第二次。这是我一直在尝试的,但我不知道这个想法是否正确。 有什么帮助吗?

def writing_line(namefilein):
    print namefilein
    filein=open(namefilein, "rw")
    tag="intro"
    filein.read()
    for line in filein:
        if tag=="second" or tag=="coord":
            try:
                filein.write("\n\n %s" %(friend))
                print line
            except:
                if tag=="coord":    
                    tag="end"
                else:
                    tag="coord"

        if "           hello" in line:
            if tag=="intro":
                tag="first"
            elif tag=="first":
                tag="second"
    filein.close()

该算法查找“hello”这个词,只是第二次找到“hello”之后写“friend”两行的单词。

2 个答案:

答案 0 :(得分:0)

可能不是最有效的,但无论你是否可以将文件读入这样的列表,并在修改列表之后用一些逻辑在所有friend之后插入hello紧随其后的friend没有。您可以将列表写回原始文件:

with open("data.txt") as f:
    content = f.readlines()

content = [x.strip() for x in content]

for idx, val in enumerate(content):
    if content[idx] == "hello" and idx != len(content) - 1:
        if content [idx + 1] != "friend":
            content.insert(idx + 1, "friend")
    elif content[idx] == "hello":
        content.insert(len(content), "friend")

f = open("data.txt", 'w')
for item in content:
    f.write("%s\n" % item)

原始data.txt:

hello
friend
hello
hello
hello

data.txt之后:

hello
friend
hello
friend
hello
friend
hello
friend

答案 1 :(得分:0)

from itertools import tee, izip_longest

def f(lines):
    new_lines = []
    lines_it, next_lines_it = tee(lines)
    next(next_lines_it) # advance iterator to second item
    h_count = 0
    for line, next_line in izip_longest(lines_it, next_lines_it):
        new_lines.append(line)
        if line == 'hello':
            h_count += 1
            if h_count == 2 and next_line != 'friend':
                new_lines.append('friend')
    return new_lines

一些例子:

>>> f(['hello'])
['hello']
>>> f(['hello', 'hello'])
['hello', 'hello', 'friend']
>>> f(['hello', 'hello', 'hello'])
['hello', 'hello', 'friend', 'hello']
>>> f(['hello', 'hello', 'friend', 'hello'])
['hello', 'hello', 'friend', 'hello']
>>> f(['hello', 'friend', 'hello', 'hello'])
['hello', 'friend', 'hello', 'friend', 'hello']

这里重写它以使用文件名:

from itertools import tee, izip_longest

def f(infilename, outfilename):
    with open(infilename, 'r') as infile, open(outfilename, 'w') as outfile:
        lines_it, next_lines_it = tee(infile)
        next(next_lines_it) # advance iterator to next line
        h_count = 0
        for line, next_line in izip_longest(lines_it, next_lines_it):
            outfile.write(line)
            if line == 'hello\n':
                h_count += 1
                if h_count == 2 and next_line != 'friend\n':
                    outfile.write('friend\n')