Skip to content Skip to sidebar Skip to footer

Trouble Separating Lists In Order To Use An Element From It

I am trying to open a file and get the first word from that file and then compare if it is the same word i.e. source_index.txt TTTGATTAAT , source-document01012.txt , 0 , 9 TAATAG

Solution 1:

This code

withopen ('source_index.txt', 'r') as fsource:
    index1 = [line.strip().split(",") for line in fsource]

returns a list of list.

So you should try index[0][0] instead of index[0]

Solution 2:

I don't know why you are looping through the entire file when you only want the first word in the first line

You only need to do this:

  • Read the first line
  • Split it by " ," (space and comma)
  • Get the first item from that split

Here's the code:

withopen ('source_index.txt', 'r') as fsource:
    index1 = fsource.readlines()[0].split(" ,")[0]
print(index1)

#TTTGATTAAT

Post a Comment for "Trouble Separating Lists In Order To Use An Element From It"