Entering edit mode
4.3 years ago
adrian18_07
▴
10
I have a NEXUS file and want to add a block to it. The file looks like this:
#NEXUS
begin data;
dimensions ntax=2 nchar=633;
format datatype=dna missing=? gap=-;
matrix
22814 CATG---GACAGAGCGACCCGCG--CACGTTACAAACACTACGCGGGGTGGCCCCGGCTGCCTCGCGCGGAGGTGCTG---CT
11246 CATG---GACAGAGCGACCCGCGAACACGTTACAAACACTACGCGGGGTGGCCCCGGCTGCCTCGC--GGAGGTGCTG--GCT
;
end;
I'm using this code:
for x in ad:
with open("nex.nexus", "a") as myfile:
myfile.write("\n" + str(x))
And gets:
#NEXUS
begin data;
dimensions ntax=2 nchar=633;
format datatype=dna missing=? gap=-;
matrix
22814 CATG---GACAGAGCGACCCGCG--CACGTTACAAACACTACGCGGGGTGGCCCCGGCTGCCTCGCGCGGAGGTGCTG---CT
11246 CATG---GACAGAGCGACCCGCGAACACGTTACAAACACTACGCGGGGTGGCCCCGGCTGCCTCGC--GGAGGTGCTG--GCT
;
end;
22814 0 0 1 0 0
11246 0 1 0 0 1
I would like the last two lines to be added before the ";" on the 8th line. And finally get:
#NEXUS
begin data;
dimensions ntax=2 nchar=633;
format datatype=dna missing=? gap=-;
matrix
22814 CATG---GACAGAGCGACCCGCG--CACGTTACAAACACTACGCGGGGTGGCCCCGGCTGCCTCGCGCGGAGGTGCTG---CT
11246 CATG---GACAGAGCGACCCGCGAACACGTTACAAACACTACGCGGGGTGGCCCCGGCTGCCTCGC--GGAGGTGCTG--GCT
22814 0 0 1 0 0
11246 0 1 0 0 1
;
end;
Thanks for any answer.
Only the
';'
character it won't always be on the 8th line.Ok, you can can use an enumerated loop through the lines to save that index to a variable for use in the above indexing step.
.strip() method will get rid of any whitespace that might mess with your equivalence test. idx can be used like so: