import string
import os,sys
file=open("C:\Python27\\New Text Document.txt",'r')\
seq =file.readlines() basecomplement = {'A': 'T', 'C': 'G', 'G': 'C', 'T': 'A'}
def translate(seq):
aaseq = []
for str in seq:
aaseq.append(basecomplement[str])
return aaseq
for frame in range(1):
rseqn= (''.join(item.split('|')[0] for item in translate(seq[frame:])))
rseqn = rseqn[::-1]
print(rseqn)
print(seq)
错误在这里是文件
"C:\Users\ram\Desktop\pythonhw\dnacomp.py", line 13, in <module>
rseqn= (''.join(item.split('|')[0] for item in
translate(seq[frame:]))) File
"C:\Users\ram\Desktop\pythonhw\dnacomp.py", line 10, in translate
aaseq.append(basecomplement[str]) KeyError:
'AGTCTGGCATACCAGTACAGACTATCA'
如果我使用简单的字符串我也得到序列但我试图读取文件作为输入它显示以下错误
答案 0 :(得分:2)
如果我理解你想要的东西
def translate(seq):
basecomplement = {'A': 'T', 'C': 'G', 'G': 'C', 'T': 'A'}
return ''.join(basecomplement[letter] for letter in seq)
测试
s = 'ATCGGCATAATAC'
>>> translate(s)
'TAGCCGTATTATG'
要翻译文件,您可以执行类似
的操作with open('file.txt', 'r') as f:
for line in f:
print(translate(line))