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

Discussion in 'Python' started by materile11, Sep 2, 2013.

  1. materile11

    materile11 Guest

    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.
     
    materile11, Sep 2, 2013
    #1
    1. Advertisements

  2. | <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, Sep 2, 2013
    #2
    1. Advertisements

  3. materile11

    Tim Chase Guest

    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]
    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
     
    Tim Chase, Sep 2, 2013
    #3
  4. materile11

    Tim Chase Guest

    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
     
    Tim Chase, Sep 2, 2013
    #4
  5. materile11

    materile11 Guest

    El domingo, 1 de septiembre de 2013 19:34:16 UTC-5, Tim Chase escribió:

    Thank you, I've used the os.path.basename to solve my problem.
    Regards.
     
    materile11, Sep 2, 2013
    #5
  6. materile11

    Ethan Furman Guest

    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 Furman, Sep 2, 2013
    #6
  7. 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.

    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 D'Aprano, Sep 3, 2013
    #7
  8. materile11

    Tim Chase Guest

    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
     
    Tim Chase, Sep 3, 2013
    #8
  9. materile11

    random832 Guest

    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.
     
    random832, Sep 3, 2013
    #9
  10. materile11

    Terry Reedy Guest

    and only for the path the the command, when needed, and not for the
    arguments of the command. Example, in a python development directory
     
    Terry Reedy, Sep 6, 2013
    #10
  11. materile11

    random832 Guest

    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.
     
    random832, Sep 6, 2013
    #11
  12. 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.
     
    Fábio Santos, Sep 11, 2013
    #12
    1. Advertisements

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 (here). After that, you can post your question and our members will help you out.