简体   繁体   English

Python:在一行中搜索STR1并将整行替换为STR2

[英]Python: search for a STR1 in a line and replace the whole line with STR2

I have a file in which I need to search for STR1 and replace the whole line containing STR2 . 我有一个文件,我需要在其中搜索STR1并替换包含STR2的整个行。 For example the file1 contains the following data 例如, file1包含以下数据

Name: John
Height: 6.0
Weight: 190
Eyes: Blue

I need to search for Name in the above file and then replace the whole line with Name: Robert . 我需要在上面的文件中搜索Name ,然后将整个行替换为Name: Robert I can accomplish this easily in sed as 我可以轻松地完成此操作

sed -i 's/.*Name.*/Name:Robert/' file1

But how to get the same in python. 但是如何在python中获得相同的结果。 For example I can replace one string with another string using fileinput as follows 例如,我可以使用fileinput将一个字符串替换为另一个字符串,如下所示

#! /usr/bin/python
import fileinput
for line in fileinput.input("file1", inplace=True):
    # inside this loop the STDOUT will be redirected to the file
    # the comma after each print statement is needed to avoid double line breaks
    print line.replace("Name: John", "Name: Robert"),

How to modify the above code to replace the whole line, using '*' replaces all the lines in the file even with a search condition ( if "Name" in line ) 如何修改以上代码以替换整行,即使使用搜索条件,使用'*'替换文件中的所有行( if "Name" in line

You can use string.find() to determine if a string is within another string. 您可以使用string.find()确定一个字符串是否在另一个字符串中。 Related Python docs . 相关的Python文档

#! /usr/bin/python
import fileinput
for line in fileinput.input("file1", inplace=True):
    if line.find("Name:") >= 0:
        print "Name: Robert"
    else:
        print line[:-1]

Should do exactly what you want. 应该正是您想要的。

def replace_basic(key_to_replace, new_value, file):
    f = open(file, 'rb').readlines()
    with open(file, 'wb') as out:
        for line in f:
            if key_to_replace in line:
                out.write(new_value+'/n') #asuming that your format never changes then uncomment the next line and comment out this one.
                #out.write('{}: {}'.format(key_to_replace, new_value))
                continue
            out.write(line)

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

相关问题 如何使用python在文件中搜索字符串(比如str1)并将其替换为另一个字符串(比如str2)? - How to search a string(say str1) and replace it with another string(say str2) in a file using python? 逻辑表达式:为什么“str1 in str2 or str2 not in str1”为我的打印语句返回 True? - Logical Expressions: Why does “str1 in str2 or str2 not in str1” return True for my print statement? <function> (str1,str2)分别调用它们 - <function>(str1, str2) calling them separately 给定 2 个字符串(DNA 序列),它会返回一个布尔值,以显示 str1 中是否存在与 str2 的片段配对的长度 &gt;=5 的连续子片段 - Given 2 strings, (DNA sequences), it retrns a bool to show if a contigus sub-fragm of length >=5 exists in str1 that pairs to a fragment of str2 Python代码含义:str.split(line [:-1],&#39;,&#39;) - Python Code meaning: str.split(line[:-1],',') Python搜索和替换行 - Python Search and Replace Line 查找并替换整行python - find and replace whole line python 在python中,如何使用__str__逐行返回列表中的元素 - In python, how to use __str__ to return elements in a list, line by line 如何在文本文件中搜索术语,然后用python中的新行替换整行? - How to search for a term in a text file then replace that whole line with a new line in python? python:我想搜索一行并替换一行 - python: I want to search a line and replace a line
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM