简体   繁体   中英

Python: Writing output to text file, but text file does not contain the entire string and truncates randomly?

I am parsing an xls file using Python then converting that information into SBML (a version of XML).

from mod2sbml import Parser

s = open('sbmltest3.mod', 'r').read()
p = Parser()
d = p.parse(s)

outfile2 = open('sbmlconvert.xml', 'w')
print >> outfile2, d.toSBML()
outfile2.close()

This is a fairly long file (>3000 lines) and when I open the .xml, the string is truncated randomly around 1400 or 3000 lines. However, when I type: print d.toSBML() and print this string to console, the string is not truncated and I can see the end of the parsed string.

What could be the problem here?

Edit: To further dissect the problem, I have closed the code with outfile2.close() and also tried to print s and print to console in my script. This returns both truncated s and d strings. However, when I type the exact commands into the interpreter separately, both print correctly. Anyone know what's going on with this discrepancy?

Try this:

from mod2sbml import Parser    
p = Parser()    

with open('sbmlconvert.xml', 'w') as of:   
    s = open('sbmltest3.mod', 'r').read()    
    d = p.parse(s)
    of.write(d.toSBML())    

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM