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

  • SEARCH
  • Home
  • 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

I am now trying to understand some code and I have found a pattern,
This is trying to insert null into Comment.BlogArticleID. The following GenericADOException appeared: could not
somewhat confused.. but trying to do a search/repace using wildcards if i have something
I am trying to create a class with two methods with the same name,
I'm going to do a wpf application using MVVM(It based on http://www.codeproject.com/KB/WPF/MVVMQuickTutorial.aspx ). This
Original Question Consider the following scenario: public abstract class Foo { public string Name
Table has surrogate primary key generated from sequence. Unfortunately, this sequence is used for
What is the normal behavior in Objective-C if you call a method on an
OK, so here's the snippet: // start rememberMe $cookie_name = 'db_auth'; $cookie_time = (3600
Does anybody know of a library or piece of software out there that will

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.