Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3489686
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T11:25:29+00:00 2026-05-18T11:25:29+00:00

s = Comment=This is a comment Name=Frank J. Lapidus GenericName=Some name replace_name = Dr.

  • 0
s = """Comment=This is a comment
Name=Frank J. Lapidus
GenericName=Some name"""
replace_name = "Dr. Jack Shephard"

I have some text in a file and have been trying to figure out how to search and replace a line so Name=Frank J. Lapidus becomes Name=Dr. Jack Shephard

How could I do this in Python? Edited: (BTW, the second element would be a \n just in case you were wondering).

Thanks.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-18T11:25:29+00:00Added an answer on May 18, 2026 at 11:25 am

    You could use the regular expression functions from the re module. For example like this:

    import re
    pattern = re.compile(r"^Name=(.*)$", flags=re.MULTILINE)
    re.sub(pattern, "Name=%s" % replace_name, s)
    

    (The re.MULTILINE option makes ^ and $ match the beginning and the end of a line, respectively, in addition to the beginning and the end of the string.)


    Edited to add: Based on your comments to Emil’s answer, it seems you are manipulating Desktop Entry files. Their syntax seems to be quite close to that used by the ConfigParser module (perhaps some differences in the case-sensitivity of section names, and the expectation that comments should be preserved across a parse/serialize cycle).

    An example:

    import ConfigParser
    parser = ConfigParser.RawConfigParser()
    parser.optionxform = str # make option names case sensitive
    parser.read("/etc/skel/examples.desktop")
    parser.set("Desktop Entry", "Name", replace_name)
    parser.write(open("modified.desktop", "w"))
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Can anyone explain what this mod_rewrite rule is doing? I'm trying to comment the
I'm trying to insert a comment character into a string something similar to this:
The comment to this answer got me wondering. I've always thought that C was
In a comment on this answer to another question , someone said that they
Comment on Duplicate Reference: Why would this be marked duplicate when it was asked
I got a comment to my answer on this thread: Malloc inside a function
This is also a question that I asked in a comment in one of
take a look at this example code: public class Comment { private Comment() {
I'm tearing my hair out with this one. If I start a block comment
I read this answer and its comments and I'm curious: Are there any reasons

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.