字典不适用于 txt 文件中的 DNA 字符串
Dictionary not working on DNA string from txt file
使用字典的函数returns当我使用文本文件中的 DNA 字符串时,KeyFile 错误“\n”,但当我手动输入 DNA 字符串时却没有:
DNA = open(r"C:\Users\CP\Desktop\Python\rosalind_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 编程新手,非常感谢任何反馈!
文件中的行有一个newline character, unlike the string you manually typed in. So you need to remove the newline character, which you can do with rstrip
,像这样:
DNA = DNA.read().rstrip()
使用字典的函数returns当我使用文本文件中的 DNA 字符串时,KeyFile 错误“\n”,但当我手动输入 DNA 字符串时却没有:
DNA = open(r"C:\Users\CP\Desktop\Python\rosalind_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 编程新手,非常感谢任何反馈!
文件中的行有一个newline character, unlike the string you manually typed in. So you need to remove the newline character, which you can do with rstrip
,像这样:
DNA = DNA.read().rstrip()