convert letters to numbers for a file in a list

I have read a file in a list which looks like this:

['>chr1_sliding:1-1000\n', 'TCATGGCTATTTTCATAAAAAATGGGGGTTGTGTGGCCATTTATCATCGACTAGAGGCTCATAAACCTCACCCCACATATGTTTCCTTGCCATAGATTACATTCTTGGATTTCTGGTGGAAACCAT\n', '\n', '>chr1_sliding:901-1900\n', 'TCATGGCTATTTTCATAAAAAATGGGGGTTGTGTGGCCATTTAT....]

I want to convert the letters to numbers based on this dictionary:

dict={"A":0,"T":1,"G":2,"C":3}

I have done this:

with open("/Users/Downloads/test") as file_in:
    lines = []
    for line in file_in:
        lines.append(line)
for line in lines:
    try:
        print(dict[line])
    except KeyError:
        print("header")

however I get "header" printed every line Output

header header header header

Correct output:

header 13012... header 13012



Read more here: https://stackoverflow.com/questions/64890672/convert-letters-to-numbers-for-a-file-in-a-list

Content Attribution

This content was originally published by Paolo Lorenzini at Recent Questions - Stack Overflow, and is syndicated here via their RSS feed. You can read the original post over there.

%d bloggers like this: