13.10.20 11:52, Tony Flury via Python-list пише: > I am trying to write a simple expression to build a raw string that ends > in a single backslash. My understanding is that a raw string should > ignore attempts at escaping characters but I get this : > > >>> a = r'end\' > File "<stdin>", line 1 > a = r'end\' > ^ > SyntaxError: EOL while scanning string literal > > I interpret this as meaning that the \' is actually being interpreted as > a literal quote - is that a bug ?
r'You can\'t end raw string literal with a single "\"' If backslash be true inner in a raw string, the above literal would end after \'. It would be very hard to write a raw string containing both \' and \", and even \''' and \""" (there are such strings in the stdlib). So you have problem either with trailing backslash, or with inner backslash followed by quotes. Both problems cannot be solved at the same time. Python parser works as it works because initially it was easier to implement, and now this cannot be changed because it would break some amount of correct code. > The only solution I have found is to do this : > > >>> a = r'end' + chr(92) > >>> a > 'end\\' > >>> list(a) > ['e', 'n', 'd', '\\'] > > or > > > >>> a = r'end\\'[:-1] > >>> list(a) > ['e', 'n', 'd', '\\'] > > Neither of which are nice. You can also write r'end' '\\'. It is not too nice, but it looks nicer to me then two other variants. -- https://mail.python.org/mailman/listinfo/python-list