Reputation: 821
how to do I read a file, replace certain letters and write the replaced letters to a file in python?
Thanks in advance.
This is how far I got
> def mutate():
> f=open("DNA.txt", "r")
> onef= open("normalDNA.txt","w+")
> twof= open("mutatedDNA.txt","w+")
>
> if f.mode == 'r':
> for line in f.readlines():
> .replace()
the DNA.txt file is the read file and contains
ACATTTGCTTCTGACACAACTGTGTTCACTAGCAACCTCAAACAGACACCATGGTGCATCTGACTCCTGa
The 'a' should be replaced with 'A' and printed to normalDNA.txt
then 'a' should be replaced again wtih 'T' and saved to another file mutatedDNA.txt
Upvotes: 1
Views: 167
Reputation: 20427
If it's simply 'a' you want to replace, you might try (in your for loop):
onef.write(line.replace('a', 'A'))
twof.write(line.replace('a', 'T'))
Remember to close your files when you're finished with them (or, even better, open them as a context manager in the first place)
Upvotes: 2