3

I have this so far.

import csv
ifile = open('file', 'rb')
reader = csv.reader(ifile,delimiter='\t')
ofile = open('file', 'wb')
writer = csv.writer(ofile, delimiter='\t')
findlist = ['A', 'G', 'C', 'T', 'Y', 'R', 'W', 'S', 'K', 'M', 'X', 'N', '-']
replacelist = ['AA', 'GG', 'CC', 'TT', 'CT', 'AG', 'AT', 'GC', 'TG', 'CA', 
'NN', 'NN', '-']
rep = dict(zip(findlist, replacelist))
def findReplace(find, replace):
 s = ifile.read()
 s = s.replace(find, replace)
 ofile.write(s)
for item in findlist:
 findReplace(item, rep[item])
ifile.close()
ofile.close() 

What it does is replaced the A with AA. However what I want is to replace all of the letters with the ones in the replacelist. I am very new to python and can't quite figure out why its not replacing everything.

HE670865 399908 N N N N N
HE670865 399910 N N N N N
HE670865 399945 T T N T T
HE670865 399951 R R N A A
HE670865 399957 A A N A A
HE670865 399978 C C C M C
HE670865 399980 C C C C C
HE670865 399982 T T T T K
HE670865 399984 C C C C C
HE670865 399908 N N N N N
HE670865 399910 N N N N N
HE670865 399945 T T N T T
HE670865 399951 R R N AA AA
HE670865 399957 AA AA N AA AA
HE670865 399978 C C C M C
HE670865 399980 C C C C C
HE670865 399982 T T T T K
HE670865 399984 C C C C C
asked Nov 3, 2013 at 0:50

1 Answer 1

6

It is because you are reading and writing inside the loop.

rep = dict(zip(findlist, replacelist))
s = ifile.read()
for item in findlist:
 s = s.replace(item, rep[item])
ofile.write(s)

Also, I think your code would be more readable (and more concise), without using the unnecessary dict.

s = ifile.read()
for item, replacement in zip(findlist, replacelist):
 s = s.replace(item, replacement)
ofile.write(s)
answered Nov 3, 2013 at 0:55
Sign up to request clarification or add additional context in comments.

2 Comments

I didn't get the reason for the question code not working. Was it because of the function usage resulting in localized variables? I tried with ifile & ofile as strings & that was the error in my code.
Since you read and wrote for every replace, ifile had already been read all the way to the end after the first iteration. So ifile.read() was returning the empty string after the first iteration.

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.