A function that utilizes a dictionary returns the KeyFile error "\n" when I use DNA strings from a text file, but not when I manually type a DNA string:
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)
I am new to Python programming, so I appreciate any feedback!
The line in the file has a newline character, unlike the string you manually typed in. So you need to remove the newline character, which you can do with rstrip
, like so:
DNA = DNA.read().rstrip()