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 6152735
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T19:58:15+00:00 2026-05-23T19:58:15+00:00

I have had written text file in which I have statements like these :

  • 0

I have had written text file in which I have statements like these :

WriteByte(0x6000, 0x28);    // Register Value ...    

WriteByte(0x6002, 0x02);    //      

WriteByte(0x6004, 0x08);    //      

I have to replace 0x28 with a user given value as an input
This means I have replace 0x28 with usr_value which may be 0x35 or 0x38 etc..
Also I also cant count on there being only 0x28 it coould be any other value whose contents are to be replaced by user given content.
Also since the text file is hand written it could have extra spaces or not

WriteByte(0x6000,0x28); // Register Value ...

or

WriteByte( 0x6000 , 0x28);  // Register Value ...

I tried using string.replace but it may not work for all combinations.
What’s the best way of doing this apart from using Regular expressions ?

  • 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-23T19:58:17+00:00Added an answer on May 23, 2026 at 7:58 pm

    From the discussion below, if you want to find all the second arguments to WriteBytes and prompt for replacing, you can do something like this:

    1. parse the file to find all the 2nd arguments to WriteBytes, using a regular expression, and store them in a set (which will handle duplicates for you)

    2. for all the values you’ve seen, prompt the user for the replacement value, and store that in a dictionary

    3. read the file again, and perform the replacements, storing the modified lines in a list, together with the unmodified lines

    4. write the data back to disk.

    Sample code:

    import re
    
    filename = '/tmp/toto.txt'
    
    write_byte_re= r'WriteByte\([^,]+,\s*([^\)]+)\)'
    
    # look for all potential substitutions
    search_values = set()
    f = open(filename)
    for line in f:
        print line
        match_object = re.match(write_byte_re, line)
        if match_object is None: # nothing found, keep looking
            continue
        else:
            search_values.add(match_object.group(1)) # record the value
    
    f.seek(0) # rewind file
    
    substitutions = {}
    for value in search_values:
        print "What do you want to replace '%s' with? (press return to keep as is)"
        new_value = raw_input('> ')
        if new_value != '': 
            substitutions[value] = new_value
    
    changed_lines = []
    for line in f:
        match_object = re.match(write_byte_re, line)
        if match_object is not None: 
            value = match_object.group(1)
            if value in substitutions: # not in the dictionary if the user said nothing
                new_value = substitutions[value]
                # modify line
                line = re.sub('\b%s\b' % value, new_value, line)
        changed_lines.append(line)
    
    f.close()
    
    # write output
    f = open(filename, 'w')
    f.writelines(changed_lines)
    f.close()
    

    You can avoid reading the file twice at the cost of a slightly more complicated code (left as an exercise to the reader)

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I had the following idea: Say we have a webapp written using django which
I have a text file that is written in C# using ascii encoding and
As I had written in title, I have SQL query, run on Oracle DB,
In Ruby I have often written a number of small classes, and had a
I have an xml file which describes (among other things) elements with attribute values
I've written the JQuery below, and have had to search for items in the
I have a text file (its comma delimited with some fields wrapped around in
I have a program written in python, and I would like to make it
I inherited a data-storage which was using simple text-files to save documents. Documents had
Have had to write my first proper multithreaded coded recently, and realised just how

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.