Friday, 23 August 2013

How do I add newline within text string to zipfile using python 2.7

How do I add newline within text string to zipfile using python 2.7

Writing text to a zipfile using python. Below if the section of code I am
using to do this. I can't figure out what character I need to use to get
it to add new lines to the zipfile.
if Count:
TextX = "The following species are present:"
blurb = "\r\nINSERT TABLE HERE\r\n\r\nSA* - South America\r\nNA** -
North America\r\nCA*** - Central America"
else:
TextX = "No species are present."
blurb = ""
Right now "if Count" is true, the output in the document looks like this:
INSERT TABLE HERE SA* - South America NA** - North America CA*** -
Central America
I want it too look like this:
INSERT TABLE HERE
SA* - South America
NA** - North America
CA*** - Central America
Below is some other pertinent script snippet that might help troubleshoot.
The script is 600+ lines long which is why I did not include the whole
thing. Everyting works except this piece.
replaceText = {"TextSpecies" : TextX,
"TEXTBLURB" : blurb}
template = zipfile.ZipFile("C:\\Template.docx")
response = zipfile.ZipFile(results, "a")
with open(template.extract("word/document.xml", databaseDir + "\\")) as
tempXmlFile:
tempXml = tempXmlFile.read()
for key in replaceText.keys():
tempXml = tempXml.replace(str(key), str(replaceText.get(key)))
with open(databaseDir + "\\temp.xml", "w+") as tempXmlFile:
tempXmlFile.write(tempXml)
for file in template.filelist:
if not file.filename == "word/document.xml":
response.writestr(file.filename, template.read(file))
response.write(databaseDir + "\\temp.xml", "word/document.xml")
response.close()
template.close()
Ideas as to how to add new lines? I tried \r, \n, \r\n, ^11. None worked.
Thanks in advance.

No comments:

Post a Comment