[Tutor] create an xls file using data from a txt file

Prasad, Ramit ramit.prasad at jpmchase.com
Wed May 11 20:39:11 CEST 2011


>your "\" is a "/"

>when writing out a string, such as 'C:\test.xls', the "/" is an escape in python. So you have two choices, You can either write
out path = 'C:\\test.xls', which will be 'C:\test.xls' or you can write out path = r'C:\test.xls' the "r" bit tells python that the following is a regular expression. or regex.


'/' is perfectly valid Windows separator. See the tested examples below. It works just fine pretty much anywhere I have ever tried it, including the command line. (except apparently for an MSOffice file save dialog that I tried just now)

>>> import xlwt
>>> workbook = xlwt.Workbook()
>>> sheet = workbook.add_sheet('test')
>>> sheet.write(0,0,'test')
>>> workbook.save('C:/test')
>>> workbook.save('C:/test.xls')
>>> workbook.save('C:\\test2.xls')
>>> workbook.save(r'C:\test3.xls')
>>>


The error he is getting may be unrelated to actually saving. I am not very familiar with the xlwt, but I know it does not write everything to file on the sheet.write() command. Save() actually does some writing and then saves the file. This can lead to misleading errors. I have had a similar error when writing non-ASCII data (and not changing the default ASCII encoding type).


Furthermore, the escape character is '\' not '/',

And r'string'  means raw string not regular expression.
"String literals may optionally be prefixed with a letter 'r' or 'R'; such strings are called raw strings and use different rules for interpreting backslash escape sequences." ~ http://docs.python.org/reference/lexical_analysis.html


Ramit



Ramit Prasad | JPMorgan Chase Investment Bank | Currencies Technology
712 Main Street | Houston, TX 77002
work phone: 713 - 216 - 5423



This communication is for informational purposes only. It is not
intended as an offer or solicitation for the purchase or sale of
any financial instrument or as an official confirmation of any
transaction. All market prices, data and other information are not
warranted as to completeness or accuracy and are subject to change
without notice. Any comments or statements made herein do not
necessarily reflect those of JPMorgan Chase & Co., its subsidiaries
and affiliates.

This transmission may contain information that is privileged,
confidential, legally privileged, and/or exempt from disclosure
under applicable law. If you are not the intended recipient, you
are hereby notified that any disclosure, copying, distribution, or
use of the information contained herein (including any reliance
thereon) is STRICTLY PROHIBITED. Although this transmission and any
attachments are believed to be free of any virus or other defect
that might affect any computer system into which it is received and
opened, it is the responsibility of the recipient to ensure that it
is virus free and no responsibility is accepted by JPMorgan Chase &
Co., its subsidiaries and affiliates, as applicable, for any loss
or damage arising in any way from its use. If you received this
transmission in error, please immediately contact the sender and
destroy the material in its entirety, whether in electronic or hard
copy format. Thank you.

Please refer to http://www.jpmorgan.com/pages/disclosures for
disclosures relating to European legal entities.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20110511/955ebc15/attachment-0001.html>


More information about the Tutor mailing list