Why can't Python's raw string literals end with a single backslash?

后端 未结 12 1180
后悔当初
后悔当初 2020-11-22 07:37

Technically, any odd number of backslashes, as described in the documentation.

>>> r\'\\\'
  File \"\", line 1
    r\'\\\'
       ^
Syn         


        
相关标签:
12条回答
  • 2020-11-22 08:09

    The reason for why r'\' is syntactical incorrect is that although the string expression is raw the used quotes (single or double) always have to be escape since they would mark the end of the quote otherwise. So if you want to express a single quote inside single quoted string, there is no other way than using \'. Same applies for double quotes.

    But you could use:

    '\\'
    
    0 讨论(0)
  • 2020-11-22 08:17

    That's the way it is! I see it as one of those small defects in python!

    I don't think there's a good reason for it, but it's definitely not parsing; it's really easy to parse raw strings with \ as a last character.

    The catch is, if you allow \ to be the last character in a raw string then you won't be able to put " inside a raw string. It seems python went with allowing " instead of allowing \ as the last character.

    However, this shouldn't cause any trouble.

    If you're worried about not being able to easily write windows folder pathes such as c:\mypath\ then worry not, for, you can represent them as r"C:\mypath", and, if you need to append a subdirectory name, don't do it with string concatenation, for it's not the right way to do it anyway! use os.path.join

    >>> import os
    >>> os.path.join(r"C:\mypath", "subfolder")
    'C:\\mypath\\subfolder'
    
    0 讨论(0)
  • 2020-11-22 08:19

    I encountered this problem and found a partial solution which is good for some cases. Despite python not being able to end a string with a single backslash, it can be serialized and saved in a text file with a single backslash at the end. Therefore if what you need is saving a text with a single backslash on you computer, it is possible:

    x = 'a string\\' 
    x
    'a string\\' 
    
    # Now save it in a text file and it will appear with a single backslash:
    
    with open("my_file.txt", 'w') as h:
        h.write(x)
    

    BTW it is not working with json if you dump it using python's json library.

    Finally, I work with Spyder, and I noticed that if I open the variable in spider's text editor by double clicking on its name in the variable explorer, it is presented with a single backslash and can be copied to the clipboard that way (it's not very helpful for most needs but maybe for some..).

    0 讨论(0)
  • 2020-11-22 08:20

    Comming from C it pretty clear to me that a single \ works as escape character allowing you to put special characters such as newlines, tabs and quotes into strings.

    That does indeed disallow \ as last character since it will escape the " and make the parser choke. But as pointed out earlier \ is legal.

    0 讨论(0)
  • 2020-11-22 08:22

    Since \" is allowed inside the raw string. Then it can't be used to identify the end of the string literal.

    Why not stop parsing the string literal when you encounter the first "?

    If that was the case, then \" wouldn't be allowed inside the string literal. But it is.

    0 讨论(0)
  • 2020-11-22 08:24

    In order for you to end a raw string with a slash I suggest you can use this trick:

    >>> print r"c:\test"'\\'
    test\
    
    0 讨论(0)
提交回复
热议问题