Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #53441 > unrolled thread
| Started by | materile11@gmail.com |
|---|---|
| First post | 2013-09-01 17:03 -0700 |
| Last post | 2013-09-11 11:16 +0100 |
| Articles | 14 — 9 participants |
Back to article view | Back to comp.lang.python
How to split with "\" character, and licence copyleft mirror of © materile11@gmail.com - 2013-09-01 17:03 -0700
Re: How to split with "\" character, and licence copyleft mirror of © Cameron Simpson <cs@zip.com.au> - 2013-09-02 10:23 +1000
Re: How to split with "\" character, and licence copyleft mirror of © Tim Chase <python.list@tim.thechases.com> - 2013-09-01 19:34 -0500
Re: How to split with "\" character, and licence copyleft mirror of © materile11@gmail.com - 2013-09-01 21:20 -0700
Re: How to split with "\" character, and licence copyleft mirror of © Tim Chase <python.list@tim.thechases.com> - 2013-09-01 19:40 -0500
Re: How to split with "\" character, and licence copyleft mirror of © Tim Roberts <timr@probo.com> - 2013-09-01 19:40 -0700
Re: How to split with "\" character, and licence copyleft mirror of © Ethan Furman <ethan@stoneleaf.us> - 2013-09-02 13:22 -0700
Re: How to split with "\" character, and licence copyleft mirror of © Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2013-09-03 02:06 +0000
Re: How to split with "\" character, and licence copyleft mirror of © Tim Chase <python.list@tim.thechases.com> - 2013-09-03 05:31 -0500
Re: How to split with "\" character, and licence copyleft mirror of © random832@fastmail.us - 2013-09-03 14:31 -0400
Re: How to split with "\" character, and licence copyleft mirror of © Tim Roberts <timr@probo.com> - 2013-09-05 20:33 -0700
Re: How to split with "\" character, and licence copyleft mirror of � Terry Reedy <tjreedy@udel.edu> - 2013-09-06 02:15 -0400
Re: How to split with "\" character, and licence copyleft mirror of © random832@fastmail.us - 2013-09-06 09:29 -0400
Re: How to split with "\" character, and licence copyleft mirror of � Fábio Santos <fabiosantosart@gmail.com> - 2013-09-11 11:16 +0100
| From | materile11@gmail.com |
|---|---|
| Date | 2013-09-01 17:03 -0700 |
| Subject | How to split with "\" character, and licence copyleft mirror of © |
| Message-ID | <c795fae4-2b38-4d81-a552-a69febdac0c0@googlegroups.com> |
Hello everybody
I'm trying to run this:
<code>
>>> a = 'E:\Dropbox\jjfsdjjsdklfj\sdfjksdfkjslkj\flute.wav'
>>> a.split('\')
SyntaxError: EOL while scanning string literal
</code>
I think that the character '\' is the problem, but unfortunately I'm developing a small app for windows and I need to show only the name of the .wav file, in this case 'flute.wav'.
I also want to know how to mirror a character, in my case this one ©, because I'll use the Copyleft http://en.wikipedia.org/wiki/Copyleft to distribute my app.
Thanks.
[toc] | [next] | [standalone]
| From | Cameron Simpson <cs@zip.com.au> |
|---|---|
| Date | 2013-09-02 10:23 +1000 |
| Message-ID | <mailman.465.1378081436.19984.python-list@python.org> |
| In reply to | #53441 |
On 01Sep2013 17:03, materile11@gmail.com <materile11@gmail.com> wrote:
| <code>
| >>> a = 'E:\Dropbox\jjfsdjjsdklfj\sdfjksdfkjslkj\flute.wav'
| >>> a.split('\')
| SyntaxError: EOL while scanning string literal
| </code>
|
| I think that the character '\' is the problem, but unfortunately I'm developing a small app for windows and I need to show only the name of the .wav file, in this case 'flute.wav'.
Firstly, you want to say '\\' for a slosh (just as you would say '\n' for a linefeed).
However, you really should use the os.path module, in particular os.path.split().
Have a read of:
http://docs.python.org/3/library/os.path.html#module-os.path
http://docs.python.org/2/library/os.path.html#module-os.path
| I also want to know how to mirror a character, in my case this
| one ©, because I'll use the Copyleft http://en.wikipedia.org/wiki/Copyleft
| to distribute my app.
Isn't that a copyright symbol? I'd have a look at the "uncidoedata" module,
myself.
Cheers,
--
Cameron Simpson <cs@zip.com.au>
Just because Unix is a multiuser system doesn't mean I want to share it with
anybody! - Paul Tomblin, in rec.aviation.military
[toc] | [prev] | [next] | [standalone]
| From | Tim Chase <python.list@tim.thechases.com> |
|---|---|
| Date | 2013-09-01 19:34 -0500 |
| Message-ID | <mailman.466.1378081991.19984.python-list@python.org> |
| In reply to | #53441 |
On 2013-09-01 17:03, materile11@gmail.com wrote:
> Hello everybody
> I'm trying to run this:
>
> <code>
> >>> a = 'E:\Dropbox\jjfsdjjsdklfj\sdfjksdfkjslkj\flute.wav'
> >>> a.split('\')
>
> SyntaxError: EOL while scanning string literal
> </code>
>
> I think that the character '\' is the problem, but unfortunately
> I'm developing a small app for windows and I need to show only the
> name of the .wav file, in this case 'flute.wav'.
To directly answer your question, you need to escape the "\" so it's
a.split('\\')
That said, it's far better to use Python's built-ins to do the
processing for you:
>>> import os
>>> print os.path.basename(a)
flute.wav
which does what you want *and* works cross-platform:
[on Linux]
>>> a = '/home/tkc/path/to/flute.wav'
>>> print os.path.basename(a)
flute.wav
> I also want to know how to mirror a character, in my case this one
> ©, because I'll use the Copyleft
This can't be done in much of a general way: Unicode doesn't specify
this character, and the URL you provided suggests combining two
Unicode characters to get ↄ⃝ Unfortunately, (1) it requires a
display that knows how to produce that, which many terminals can't;
and (2) it's purely visual, not semantic. If that's what you really
want, you should be able to use:
copyleft_symbol = u"\u2184\u20DD"
Just be aware that it may not always display the way you expect it to.
-tkc
[toc] | [prev] | [next] | [standalone]
| From | materile11@gmail.com |
|---|---|
| Date | 2013-09-01 21:20 -0700 |
| Message-ID | <b70a6f24-c38f-45ad-bdb2-b9c5458ce150@googlegroups.com> |
| In reply to | #53443 |
El domingo, 1 de septiembre de 2013 19:34:16 UTC-5, Tim Chase escribió:
> On 2013-09-01 17:03, materile11@gmail.com wrote:
>
> > Hello everybody
>
> > I'm trying to run this:
>
> >
>
> > <code>
>
> > >>> a = 'E:\Dropbox\jjfsdjjsdklfj\sdfjksdfkjslkj\flute.wav'
>
> > >>> a.split('\')
>
> >
>
> > SyntaxError: EOL while scanning string literal
>
> > </code>
>
> >
>
> > I think that the character '\' is the problem, but unfortunately
>
> > I'm developing a small app for windows and I need to show only the
>
> > name of the .wav file, in this case 'flute.wav'.
>
>
>
> To directly answer your question, you need to escape the "\" so it's
>
>
>
> a.split('\\')
>
>
>
> That said, it's far better to use Python's built-ins to do the
>
> processing for you:
>
>
>
> >>> import os
>
> >>> print os.path.basename(a)
>
> flute.wav
>
>
>
> which does what you want *and* works cross-platform:
>
>
>
> [on Linux]
>
> >>> a = '/home/tkc/path/to/flute.wav'
>
> >>> print os.path.basename(a)
>
> flute.wav
>
>
>
> > I also want to know how to mirror a character, in my case this one
>
> > ©, because I'll use the Copyleft
>
>
>
> This can't be done in much of a general way: Unicode doesn't specify
>
> this character, and the URL you provided suggests combining two
>
> Unicode characters to get ↄ⃝ Unfortunately, (1) it requires a
>
> display that knows how to produce that, which many terminals can't;
>
> and (2) it's purely visual, not semantic. If that's what you really
>
> want, you should be able to use:
>
>
>
> copyleft_symbol = u"\u2184\u20DD"
>
>
>
> Just be aware that it may not always display the way you expect it to.
>
>
>
> -tkc
Thank you, I've used the os.path.basename to solve my problem.
Regards.
[toc] | [prev] | [next] | [standalone]
| From | Tim Chase <python.list@tim.thechases.com> |
|---|---|
| Date | 2013-09-01 19:40 -0500 |
| Message-ID | <mailman.467.1378082363.19984.python-list@python.org> |
| In reply to | #53441 |
On 2013-09-02 10:23, Cameron Simpson wrote: > | I also want to know how to mirror a character, in my case this > | one ©, because I'll use the Copyleft > http://en.wikipedia.org/wiki/Copyleft | to distribute my app. > > Isn't that a copyright symbol? I'd have a look at the "uncidoedata" > module, myself. Thanks to his link (which would have been more helpful with the URL fragment: http://en.wikipedia.org/wiki/Copyleft#Symbol ), I suspect the he means that it should be "the mirror image of a copyright symbol". And that would be "unicodedata", not "uncidoedata" (I don't think that was the reversing he was talking about ;-) -tkc
[toc] | [prev] | [next] | [standalone]
| From | Tim Roberts <timr@probo.com> |
|---|---|
| Date | 2013-09-01 19:40 -0700 |
| Message-ID | <r4u729t37qllb5dfcc7qmq5hpsi8apl6gf@4ax.com> |
| In reply to | #53441 |
materile11@gmail.com wrote:
>Hello everybody
>I'm trying to run this:
>
><code>
>>>> a = 'E:\Dropbox\jjfsdjjsdklfj\sdfjksdfkjslkj\flute.wav'
>>>> a.split('\')
>
>SyntaxError: EOL while scanning string literal
></code>
>
>I think that the character '\' is the problem, but unfortunately I'm
>developing a small app for windows and I need to show only the name
>of the .wav file, in this case 'flute.wav'.
I assume you know that backslash has a special meaning in string constants.
For example the string '\n\r' contains exactly two characters, and no
backslashes.
When you want to use an actual backslash in an ordinary string constant,
you have to double it. So, you could have written your code as:
a = 'E:\\Dropbox\\jjfsdjjsdklfj\\sdfjksdfkjslkj\\flute.wav'
a.split('\\')
Another altrnative is to use "raw" strings, in which backslashes are not
interpreted:
a = r'E:\Dropbox\jjfsdjjsdklfj\sdfjksdfkjslkj\flute.wav'
a.split(r'\')
I assume your filename is actually input to your program, and not as a
constant in your code, so that may not be a problem. However, there is an
API to do exactly what you're asking:
>>> import os
>>> a=r'E:\Dropbox\one\two\three\flute.wav'
>>> os.path.split(a)
('E:\\Dropbox\\one\\two\\three', 'flute.wav')
>>> os.path.split(a)[1]
'flute.wav'
>>>
>I also want to know how to mirror a character, in my case this one ©,
>because I'll use the Copyleft http://en.wikipedia.org/wiki/Copyleft
>to distribute my app.
You can't "mirror" a character. That is an invented glyph that is not
present in Unicode. Fortunately, the character doesn't have any legal
meaning, so you can just include explanatory text in your description that
identifies your license.
--
Tim Roberts, timr@probo.com
Providenza & Boekelheide, Inc.
[toc] | [prev] | [next] | [standalone]
| From | Ethan Furman <ethan@stoneleaf.us> |
|---|---|
| Date | 2013-09-02 13:22 -0700 |
| Message-ID | <mailman.513.1378154810.19984.python-list@python.org> |
| In reply to | #53450 |
On 09/01/2013 07:40 PM, Tim Roberts wrote:
>
> Another altrnative is to use "raw" strings, in which backslashes are not
> interpreted:
> a = r'E:\Dropbox\jjfsdjjsdklfj\sdfjksdfkjslkj\flute.wav'
> a.split(r'\')
Not quite.
--> r'\'
File "<stdin>", line 1
r'\'
^
SyntaxError: EOL while scanning string literal
In a raw string, the backslash is buggy (IMNSHO) when it's the last character. Given the above error, you might think
that to get a single-quote in a string delimited by single-quotes that you would use r'\'', but no:
--> r'\''
"\\'"
you get a backslash and a single-quote. And if you try to escape the backslash to get only one?
--> r'\\'
'\\\\'
You get two. Grrrr.
--
~Ethan~
[toc] | [prev] | [next] | [standalone]
| From | Steven D'Aprano <steve+comp.lang.python@pearwood.info> |
|---|---|
| Date | 2013-09-03 02:06 +0000 |
| Message-ID | <5225443e$0$6599$c3e8da3$5496439d@news.astraweb.com> |
| In reply to | #53526 |
On Mon, 02 Sep 2013 13:22:37 -0700, Ethan Furman wrote:
> In a raw string, the backslash is buggy (IMNSHO) when it's the last
> character. Given the above error, you might think that to get a
> single-quote in a string delimited by single-quotes that you would use
> r'\'', but no:
>
> --> r'\''
> "\\'"
You get exactly what you asked for. It's a raw string, right, so
backslash has no special powers, and "backslash C" should give you
exactly backslash followed by C, for any character C. Which is exactly
what you do get. So that's working correctly, as far as it goes.
> you get a backslash and a single-quote. And if you try to escape the
> backslash to get only one?
>
> --> r'\\'
> '\\\\'
>
> You get two. Grrrr.
Again, working as expected. Since backslash has no special powers, if you
enter a string with backslash backslash, you ought to get two
backslashes. Just as you do.
The *real* mystery is how the first example r'\'' succeeds in the first
place, and that gives you a clue as to why r'\' doesn't. The answer is
discussed in this bug report:
http://bugs.python.org/issue1271
Summarising, the parser understands backslash as an escape character, and
when it scans the string r'\'' the backslash escapes the inner quote, but
then when Python generates the string it skips the backslash escape
mechanism. Since the parser knows that backslash escapes, it fails to
parse r'\' and you get a SyntaxError. If you stick stuff at the end of
the line, you get the SyntaxError at another place:
py> s = r'\'[:] # and more
File "<stdin>", line 1
s = r'\'[:] # and more
^
SyntaxError: EOL while scanning string literal
So the real bug is with the parser.
It is likely that nobody noticed this bug in the first place because the
current behaviour doesn't matter for regexes, which is the primary
purpose of raw strings. You can't end a regex with an unescaped
backslash, so r'abc\'' is an illegal regex and it doesn't matter if you
can't create it.
--
Steven
[toc] | [prev] | [next] | [standalone]
| From | Tim Chase <python.list@tim.thechases.com> |
|---|---|
| Date | 2013-09-03 05:31 -0500 |
| Message-ID | <mailman.1.1378204256.5461.python-list@python.org> |
| In reply to | #53551 |
On 2013-09-03 02:06, Steven D'Aprano wrote: >> So the real bug is with the parser. > > It is likely that nobody noticed this bug in the first place > because the current behaviour doesn't matter for regexes, which is > the primary purpose of raw strings. You can't end a regex with an > unescaped backslash, so r'abc\'' is an illegal regex and it doesn't > matter if you can't create it. I'd contend that the two primary purposes of raw strings (this is starting to sound like a Spanish Inquisition sketch) are regexes and DOS/Win32 file path literals. And I hit this trailing-backslash case all the time, as Vim's path-completion defaults to putting the trailing backslash at the end. So I might be entering a literal like r"c:\win and hit <tab> which expands to r"c:\Windows\ for which I then need to both remove the backslash and close the quote. If Python's parser just blithely ignored terminal backslashes, I could just close the quote and get on with my day. -tkc
[toc] | [prev] | [next] | [standalone]
| From | random832@fastmail.us |
|---|---|
| Date | 2013-09-03 14:31 -0400 |
| Message-ID | <mailman.10.1378233513.5461.python-list@python.org> |
| In reply to | #53551 |
On Tue, Sep 3, 2013, at 6:31, Tim Chase wrote: > I'd contend that the two primary purposes of raw strings (this is > starting to sound like a Spanish Inquisition sketch) are regexes and > DOS/Win32 file path literals. And I hit this trailing-backslash case > all the time, as Vim's path-completion defaults to putting the > trailing backslash at the end. So I might be entering a literal like > > r"c:\win > > and hit <tab> which expands to > > r"c:\Windows\ > > for which I then need to both remove the backslash and close the > quote. If Python's parser just blithely ignored terminal > backslashes, I could just close the quote and get on with my day. Of course, in 99% of situations where you can use a windows pathname in Python, you are free to use it with a forward slash instead of a backslash. The fact that you're using vim's file completion, which automatically normalizes the path separator, is why you're running into this issue when other people may not. Maybe enabling the 'shellslash' option (which changes it to use forward slash) will help you, though you should be aware this also affects the expansion of the % variable, even in the :! command, which can cause problems with certain usage patterns.
[toc] | [prev] | [next] | [standalone]
| From | Tim Roberts <timr@probo.com> |
|---|---|
| Date | 2013-09-05 20:33 -0700 |
| Message-ID | <84ji2914grkpljfom0kihfpfvlo33bssbo@4ax.com> |
| In reply to | #53576 |
random832@fastmail.us wrote: > >Of course, in 99% of situations where you can use a windows pathname in >Python, you are free to use it with a forward slash instead of a >backslash. This is actually worth repeating, because it's not well known. ALL Windows APIs handle forward and backward slashes interchangably. It is only the command interpreter that requires the backslash. -- Tim Roberts, timr@probo.com Providenza & Boekelheide, Inc.
[toc] | [prev] | [next] | [standalone]
| From | Terry Reedy <tjreedy@udel.edu> |
|---|---|
| Date | 2013-09-06 02:15 -0400 |
| Subject | Re: How to split with "\" character, and licence copyleft mirror of � |
| Message-ID | <mailman.114.1378448173.5461.python-list@python.org> |
| In reply to | #53753 |
On 9/5/2013 11:33 PM, Tim Roberts wrote: > random832@fastmail.us wrote: >> >> Of course, in 99% of situations where you can use a windows pathname in >> Python, you are free to use it with a forward slash instead of a >> backslash. > > This is actually worth repeating, because it's not well known. > > ALL Windows APIs handle forward and backward slashes interchangably. It is > only the command interpreter that requires the backslash. and only for the path the the command, when needed, and not for the arguments of the command. Example, in a python development directory > pcbuild\python_d tools/scripts/patchcheck.py -- Terry Jan Reedy
[toc] | [prev] | [next] | [standalone]
| From | random832@fastmail.us |
|---|---|
| Date | 2013-09-06 09:29 -0400 |
| Message-ID | <mailman.126.1378474142.5461.python-list@python.org> |
| In reply to | #53753 |
On Thu, Sep 5, 2013, at 23:33, Tim Roberts wrote: > random832@fastmail.us wrote: > > > >Of course, in 99% of situations where you can use a windows pathname in > >Python, you are free to use it with a forward slash instead of a > >backslash. > > This is actually worth repeating, because it's not well known. > > ALL Windows APIs handle forward and backward slashes interchangably. It > is only the command interpreter that requires the backslash. Technically, that's not strictly true. There are certain strings you can open that will only work with backslashes, relating to device paths and/or the magic \\?\ prefix that removes the PATH_MAX limit (CreateFileW only). That was what I meant by 99%. And many situations in the command interpreter that require a backslash can be used with forward slash by surrounding the string in quotes, which you need to do anyway when you have an arbitrary string that may contain spaces.
[toc] | [prev] | [next] | [standalone]
| From | Fábio Santos <fabiosantosart@gmail.com> |
|---|---|
| Date | 2013-09-11 11:16 +0100 |
| Subject | Re: How to split with "\" character, and licence copyleft mirror of � |
| Message-ID | <mailman.235.1378894600.5461.python-list@python.org> |
| In reply to | #53753 |
[Multipart message — attachments visible in raw view] — view raw
On 6 Sep 2013 07:18, "Terry Reedy" <tjreedy@udel.edu> wrote: > > On 9/5/2013 11:33 PM, Tim Roberts wrote: >> >> random832@fastmail.us wrote: >>> >>> >>> Of course, in 99% of situations where you can use a windows pathname in >>> Python, you are free to use it with a forward slash instead of a >>> backslash. >> >> >> This is actually worth repeating, because it's not well known. >> >> ALL Windows APIs handle forward and backward slashes interchangably. It is >> only the command interpreter that requires the backslash. > > > and only for the path the the command, when needed, and not for the arguments of the command. Example, in a python development directory > > > pcbuild\python_d tools/scripts/patchcheck.py > Interesting. I was pretty sure that forward slashes were allowed in this situation, just that tab completion didn't work unless you used backslashes. Well, fortunately I'm not able to check that these days.
[toc] | [prev] | [standalone]
Back to top | Article view | comp.lang.python
csiph-web