python - String manipulation (replace and reverse) using a function -
All!
Newbie here! I was trying to create a function that would allow me to change the four letters (ATCG). To be more concise, I wanted to replace 'A' with 'G' and 'C' with 'G', and vice versa.
I still have the code, but I am getting an error (expected translation of at least 1 argument, got 0).
> #! / Usr / bin / python from sys import argv os.path import from import () is: str.replace ("at"); Str.replace ("C", "G"); Str.replace ("G", "C"); Str.replace ("T", "A"); Script, from_file, to_file = argv print "copy from% s to% s"% (from_file, to_file) in_file = open (from_file) indata = in_file.read () newdata = indata.translate () out_file = open (to_file, W ') out_file.write (newdata [:: - 1]) out_file.close () in_file.close ()
I tried to argue the translation function ( def Translation (Str)
) and call it with str ( newdata = indata.translate (str)
), but they do not work either.
Appreciate any help and use the guidance table:
import string table =
string .mocket ('ACGT', 'TGCA') You can apply changes like this:
with open (to_file) - f : Content = F.read () content = content.translate (table) # swap a & lt; - & gt; T and C & lt; - & gt; G content = content [:: - 1] # Reverse string with open (to_file, 'w') as F: f.write (content)
Comments
Post a Comment