How to split with "\" character, and licence copyleft mirror of ©

M

materile11

Hello everybody
I'm trying to run this:


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.
 
C

Cameron Simpson

| <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,
 
T

Tim Chase

Hello everybody
I'm trying to run this:



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:
flute.wav

which does what you want *and* works cross-platform:

[on Linux]
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
 
T

Tim Chase

| 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
 
M

materile11

El domingo, 1 de septiembre de 2013 19:34:16 UTC-5, Tim Chase escribió:
Hello everybody
I'm trying to run this:



SyntaxError: EOL while scanning string literal


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:



flute.wav



which does what you want *and* works cross-platform:



[on Linux]
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.
 
E

Ethan Furman

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.
 
S

Steven D'Aprano

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.
 
T

Tim Chase

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
 
R

random832

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.
 
T

Terry Reedy

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
 
R

random832

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.
 
F

Fábio Santos

and only for the path the the command, when needed, and not for the
arguments of the command. Example, in a python development directory
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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,994
Messages
2,570,223
Members
46,810
Latest member
Kassie0918

Latest Threads

Top