字典不工作的DNA字符串从txt文件



使用字典的函数返回KeyFile错误"n"当我使用DNA字符串从文本文件,,但不是当我手动输入DNA字符串:


DNA = open(r"C:UsersCPDesktopPythonrosalind_revc.txt","r")
DNA = DNA.read()
DNA.replace(' ','')
print(DNA)
# Output works when I manually type a DNA string, such as the one below in the comment
# DNA = "TCAGCAT"
def complement(s): 

# Create a dictionary with all the complementary base pairings
basecomplement = {'A':'T','C':'G','G':'C','T':'A'}

# Turn the input DNA into a list
letters = list(s)

# Use the dictionary to replace each list element
n = [basecomplement[b] for b in letters]

# Make the function return the list as a string
# We want no spaces in the new DNA string, so there are no spaces inside
# the quotation marks--that's where the "separator" is.
return ''.join(n)
def revcom(s):
return(complement(s[::-1]))
DNA_revcom = revcom(DNA)
print(DNA_revcom)

我是Python编程的新手,所以我感谢任何反馈!

文件中的行有一个换行符,这与您手动输入的字符串不同。因此,您需要删除换行符,可以使用rstrip,如下所示:

DNA = DNA.read().rstrip()

相关内容

  • 没有找到相关文章

最新更新