[Tutor] create an xls file using data from a txt file
Steve Willoughby
steve at alchemy.com
Wed May 11 21:48:05 CEST 2011
On 11-May-11 12:14, James Reynolds wrote:
> Actually, I never knew that about the windows separators, since I've
> just always used the '\' out of habit.
If you want your code to run everywhere, you should use the functions in
os.path to manipulate and build paths.
Otherwise, using \ all the time means your code will ONLY ever work on
Windows. Using / all the time means your code will work fine on Mac OS
X, Linux, or other POSIX systems, and PROBABLY ok on Windows most of the
time, but not on other systems.
> 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.
Not to be too pedantic, but since this is a tutorial list, I'll point
out the more accurate answer so new programmers don't get a mistaken
impression.
The 'r' string prefix does not actually mean regular expressions. It
means "raw string" where (almost) no backslash codes are recognized in
the string constant, so you could say r'C:\test.xls' instead of
'c:\\test.xls'.
Raw strings are, however, really useful for strings which hold regular
expressions, so you see them in that context a lot.
... ah... and I just noticed that this was pointed out later in the
thread. Sorry for the repeat there.
> ‘/’ 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)
Not... quite. / is accepted by a number of programs, including the
Python interpreter, which came from Unix-like systems where / is the
directory separator. Very old versions of MSDOS could be configured to
use / on the command line for pretty much everything, but that has been
deprecated for a long time now (they originally wanted / for
command-line switches instead, so used \ for directory separators).
Core windows commands don't generally accept it, including native
Windows applications (although sometimes they're lenient in what they
accept). It'll work for command-line Python script usage because it's
*python* that allows them, not *windows*.
--
Steve Willoughby / steve at alchemy.com
"A ship in harbor is safe, but that is not what ships are built for."
PGP Fingerprint 4615 3CCE 0F29 AE6C 8FF4 CA01 73FE 997A 765D 696C
More information about the Tutor
mailing list