Search code examples
pythonlistformattingstrip

strip ' from all members in a list


Ok, so I converted each line in a text file into a member of a list by doing the following:
chkseq=[line.strip() for line in open("sequence.txt")]
So when I print chkseq I get this:
['3','3']
What I would like is for it to instead look like this:
[3,3]
I know this is possible, I'm just unsure of how! I need them to be intergers, not strings. So if all else fails, that is my main goal in this: create a list from a .txt file whose members are intergers (which would be all the .txt file contained).
Thanks!! -OSFTW


Solution

  • It looks like you want to interpret the strings as integers. Use int to do this:

    chkseq = [int(line) for line in open("sequence.txt")] 
    

    It can also be written using map instead of a list comprehension:

    chkseq = map(int, open("sequence.txt"))