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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T23:50:53+00:00 2026-05-30T23:50:53+00:00

I have a text file that I am reading in python . I’m trying

  • 0

I have a text file that I am reading in python . I’m trying to extract certain elements from the text file that follow keywords to append them into empty lists . The file looks like this:

enter image description here

so I want to make two empty lists

  • 1st list will append the sequence names

  • 2nd list will be a list of lists which will include be in the format [Bacteria,Phylum,Class,Order, Family, Genus, Species]

    most of the organisms will be Uncultured bacterium . I am trying to add the Uncultured bacterium with the following IDs that are separated by ;

Is there anyway to scan for a certain word and when the word is found, take the word that is after it [separated by a ‘\t’] ?

I need it to create a dictionary of the Sequence Name to be translated to the taxonomic data .

I know i will need an empty list to append the names to:

seq_names=[ ]

a second list to put the taxonomy lists into

taxonomy=[ ]

and a 3rd list that will be reset after every iteration

temp = [ ]

I’m sure it can be done in Biopython but i’m working on my python skills

  • 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-30T23:50:54+00:00Added an answer on May 30, 2026 at 11:50 pm

    Yes there is a way.

    You can split the string which you get from reading the file into an array using the inbuilt function split. From this you can find the index of the word you are looking for and then using this index plus one to get the word after it. For example using a text file called test.text that looks like so (the formatting is a bit weird because SO doesn’t seem to like hard tabs).

    one two three   four    five    six seven   eight   nine
    

    The following code

    f = open('test.txt','r')
    
    string = f.read()
    
    words = string.split('\t')
    ind = words.index('seven')
    desired = words[ind+1]
    

    will return desired as ‘eight’

    Edit: To return every following word in the list

    f = open('test.txt','r')
    
    string = f.read()
    words = string.split('\t')
    
    desired = [words[ind+1] for ind, word in enumerate(words) if word == "seven"]
    

    This is using list comprehensions. It enumerates the list of words and if the word is what you are looking for includes the word at the next index in the list.

    Edit2: To split it on both new lines and tabs you can use regular expressions

    import re
    f = open('testtest.txt','r')
    
    string = f.read()
    
    words = re.split('\t|\n',string)
    
    desired = [words[ind+1] for ind, word in enumerate(words) if word == "seven"]
    
    • 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 is encoded in UTF-8. I'm reading it in
I have a text file that I am reading. It has chunks that I
I have a loop that's reading over a text file. Each time the loop
I have a simple Excel 2007 Macro that is reading a text file line
I have Text file that contains data separated with a comma , . How
I have a text file that contains a long list of entries (one on
I have a text file that contains localized language strings that is currently encoded
I have a text file that I want to edit using Java. It has
I have a text file that contains a list of filenames, minus the extension,
I have a text file that looks a bit like: random text random text,

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.