我有一個文件,該文件看起來像這樣:存儲一列文件爲詞典
>Organism1
ETTGDMND
>Organism2
PDELMESPEER
>Organism3
YERLLRRAQ
>Organism1
EDLTEVSGIGC
我想創建一個字典,其中大寫字母(=氨基酸序列)是鍵,有機體名稱是價值。 到目前爲止,我有:
dict1 = {}
for line in file.readlines():
line = line.rstrip()
if ">" not in line: # '>' not in the line=amino acid seq
key = line #assign the line into a variable 'key'
dict1[key] = [] #make this variable the keys of dict1
else: #if '>'is in the line = organism
value = line
dict1[key] = value
print dict1
它提出的是「鑰匙」沒有定義錯誤消息。但我認爲這是通過說key = line ..?
使用相同輸入文件的相關問題。如果我想僅在從該文件(用於其他目的)的氨基酸序列來調用,我確實:
my_sequences = []
for line in file:
line = line.rstrip()
if ">" not in line:
my_sequences = [line] # add these dna sequences to the list "my_sequences"
print my_sequences
但只打印一個序列,而不是所有的序列。 任何人都可以幫助我嗎? 謝謝!
哪個先到位,關鍵還是值? –
您的第一行是_> Organism1_。這意味着該代碼將遵循沒有定義「key」的'else'分支。 – CristiFati
啊,這是有道理的! – ccaarroo