开发者

python writing a list to a file incorrectly

I am having an issue with writing a list to a file. I am annotating certain files to change them into a certain format, so I read sequence alignment files, store them in lists, do necessary formatting,开发者_开发问答 and then write them to a new file. The problem is that while my list, containing sequence alignments is structured correctly, the output produced when it writes them to new files is incorrect (it does not replicate my list structure). I include only a section of my output and what it should look like because the list itself if far too long to post.

OUTPUT WRITTEN TO FILE:

>
TRFE_CHICK 

From XALIGN

MKLILCTVLSLGIAAVCFAAP (seq spans multiple lines) ...

ADYIKAVSNLRKCS--TSRLLEAC*> (end of sequence, * should be on a newline, followed by > on a newline as well)

OUTPUT IS SUPPOSED TO BE WRITTEN AS:

>

TRFE_CHICK

From XALIGN

MKLILCTVLSLGIAAVCFAAP (seq spans many lines) ...

ADYIKAVSNLRKCS--TSRLLEAC

*

>

It does this misformatting multiple times over. I have tried pickling and unpickling the list but that misformats it further.

My code for producing the list and writing to file:

new = []
for line in alignment1:
    if line.endswith('*\n'):
        new.append(line.strip('*\n'))
        new.append('*')
   else:
        new.append(line)

new1 = []
for line in new:
    if line.startswith('>'):
        twolines = line[0] + '\n' + line[1:]
        new1.append(twolines)
        continue
    else:
        new1.append(line)

for line in new1:
     alignfile_annot.write(line)

Basically, I have coded it so that it reads the alignment file, inserts a line between the end of the sequence and the * character and also so that > followed by the ID code are always on new lines. This is the way my list is built but not the way it is written to file. Anyone know why the misformatting? Apologies for the long text, I tried to keep it as short as possible to make my issue clear I'm running Python 2.6.5


new.append(line.strip('*\n'))
new.append('*')

You have a list of lines (with newline terminators each), so you need to include \n for these two lines, too:

new.append(line[:-2] + "\n")  # slice as you just checked line.endswith("*\n")
new.append("*\n")

Remember the strip (or slice, as I've changed it to) will remove the newline, so splitting a single item in the list with a value of "...*\n" into two items of "..." and "*" actually removes a newline from what you had originally.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜