I'm very new to python. I know the basic formula, but I don't really know any commands. I'm learning, but I have to get this done faster than I can learn. I have a tab-delimited file that looks like this:
A R R R A
R W R W R
R M S W K
etc.
and want to generate a new file where I replace certain letters under certain conditions, i.e.:
if all cells in a row are A or R: replace R with G in that row only
if all cells in a row are R and W: replace R with G and W with T in that row only
(+20 other conditions)
so that it looks like:
A G G G A
G T G T G
R M S W K
Nothing should happen if a row has 3 or more unique characters.
The concept would be to read through the first row, see if it matches any conditions, and if it does, write the new output to a file. Iterate through all rows.
file = open(file.txt)
num_columns=numcolumns() # I can manually put in the number of columns for each run, though I'm sure there's a simple command.
for line in file:
if line.contains('A') and line.contains('R') and (count('A')+count('R')==num_columns()):
write.to.newfile.replace('R') with ('G') #I obviously have no idea how to code this
elif:
new_condition #etc. etc. etc.
else:
write line to file as is
Thanks for any help and sorry for the noob question!
Please use
ADD COMMENT
to answer to earlier replies, as such this thread remains logically structured and easy to follow.