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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T19:39:48+00:00 2026-06-18T19:39:48+00:00

I have a text file that has a bunch of data in it. At

  • 0

I have a text file that has a bunch of data in it. At certain instances of this text file, I am trying to replace the follow letters in it: A->G, C->T, etc. Basically, I know I need to read the file. Search through the lines of the file. Find the occurrences of these characters and then replace. Basically –> ACTG should be come GTCA.

My code so far is as follows:

f = open("actg.txt", "r")

table = str.maketrans("actgACTG", "gtcagtca")

print(f.read().translate(str.maketrans('ACTGactg','gtcagtca')))


print(table)

This output here is working properly. However, it’s changing ALL instances of such letters in the entire file.

What if I only want the characters to change like this when they’re in this particular sequence? Otherwise, as it is, it changes every ‘a’ and every ‘g’ in the entire file. I would like to keep normal sentences intact and only have this change made when this particular sort of sequence is present.

  • 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-06-18T19:39:49+00:00Added an answer on June 18, 2026 at 7:39 pm

    You can use str.translate in combination with string.maketrans (Python 2.x) or str.maketrans (Python 3.x):

    Python 2.x:

    >>> import string
    >>> instring = 'ABCD'
    >>> instring.translate(string.maketrans('ACac','gtgt'))
    'gBtD'
    

    Python 3.x:

    >>> instring = 'ABCD'
    >>> instring.translate(str.maketrans('ACac','gtgt'))
    'gBtD'
    

    translate requires a 256 character mapping table. This is what string.maketrans creates, mapping each character to itself, except for characters in the first argument string which are mapped to the corresponding character in the second argument string.

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

Sidebar

Related Questions

I have a text file that contains cached data in JSON format. I'm trying
I have a rather large text file that has a bunch of missing newlines,
i have a text file that has a bunch of links that are are
I have a text file that has item numbers in it (one per line).
I currently have a text file that has the following: 1 Commercial & Enterprise
I have a text file that I want to edit using Java. It has
i have a java ee project which has a text file that uses a
So I have a multipart form that has fields for text, integers and file
I have Text file that contains data separated with a comma , . How
I have a text file that looks like this. A 102 B 456 C

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.