简体   繁体   English

在python中搜索特定单词后,将一个单词添加到文本文件中

[英]Append a text word into a text file after searching for specific word in python

i want to read a text file and want to a specific word and then want to append some other word next to it. 我想阅读一个文本文件,想要一个特定的词,然后在其旁边附加其他一些词。

For example: 例如:

I want to find first name in a file like John and then want to append last name with "John" like John Smith. 我想在像John这样的文件中找到名字,然后在像John Smith这样的“ John”后面加上姓氏。

Here is the code i have written up till now. 这是我到目前为止编写的代码。

usrinp = input("Enter name: ")

lines = []
with open('names.txt','rt') as in_file:
    for line in in_file:
        lines.append(line.rstrip('\n'))



    for element in lines:
        if usrinp in element is not -1:
            print(lines[0]+" Smith")
        print(element)

Thats what text file looks like: 那就是文本文件的样子:

My name is FirstName
My name is FirstName
My name is FirstName
FirstName is a asp developer
Java developer is FirstName
FirstName is a python developer
search_string = 'john'
file_content = open(file_path,'r+')
lines = []
flag = 0
for line in file_content:
    line = line.lower()
    stripped_line = line
    if search_string in line:
        flag = 1
        stripped_line = line.strip('\n')+' '+'smith \n'    
    lines.append(stripped_line)
file_content.close()
if(flag == 1):
    file_content = open(file_path,'w')
    file_content.writelines(lines)
    file_content.close()

**OUTPUT**

My name is FirstName

My name is FirstName

My name is FirstName

FirstName is a asp developer

Java developer is john smith

FirstName is a developer 

Using replace is one way to do it. 使用replace是一种方法。

Input file (names.txt): 输入文件(names.txt):

My name is John
My name is John
My name is John
John is a asp developer
Java developer is John
John is a python developer

Script: 脚本:

name = 'John'
last_name = 'Smith'

with open('names.txt','r') as names_file:
    content = names_file.read()
    new = content.replace(name, ' '.join([name, last_name]))

with open('new_names.txt','w') as new_names_file:
    new_names_file.write(new)

Output file (new_names.txt): 输出文件(new_names.txt):

My name is John Smith
My name is John Smith
My name is John Smith
John Smith is a asp developer
Java developer is John Smith
John Smith is a python developer

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

相关问题 如何打开文本文件并查找在一行中的特定单词和 append 文件名之后写入的内容到 Python 中的列表 - How to open a text file and find what is written after a specific word in a line and append that files name to a list in Python 在文本文件中搜索python中输入的单词 - searching a text file for an inputted word in python 删除 python 中的文本文件中的特定单词 - Deleting a specific word form a text file in python 如何在Python中的文本文件中搜索特定单词 - How to search a text file for a specific word in Python 用Python将文本文件中的单词替换为特定列中的单词 - Replace word in text file comma separated with word in a specific column with Python Python在文本文件中搜索确切的单词/短语 - Python searching for exact word/phrase within a text file PYTHON:在文本文件中搜索单词之间包含空格的单词 - PYTHON: searching for a word in a text file which includes spaces in between words 将下一个单词添加到python中文本文件的列表中,并按列打印 - Append next word into a list from a text file in python and print it columnwise Python在一行中查找单词并将其附加到新的文本文件中 - Python find word in a rows and append this in a new text file 从单词列表到文本文件中的单词搜索 - Searching from a list of word to words in a text file
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM