On 2020-10-17 21:03:26 -0000, Mladen Gogala via Python-list wrote:
> On Thu, 15 Oct 2020 21:30:15 +0000, Stefan Ram wrote:
> > Tony Flury <
tony....@btinternet.com> writes:
> >> >>> a = r'end' + chr(92)
> >
> > Or maybe,
> >
> > a = r'''
> > end\
> > '''[ 1: -1 ]
> >
> > ? The first and the last line are messy, but in the middle,
> > the intended string is clearly visible.
>
>
> You can use perl module for python.
Ah, I see, that the sillyness of Perl's grammar-altering modules (which
let you write Perl in Latin (with proper declensions and conjugations,
of course) or Chinese) has found its way to Python :-)
0x1C isn't a backslash, it's a control character (FS - File Separator).
0x5C is a backslash, but of course that doesn't work here either,
because the second argument to re.sub isn't a simple string: It contains
escape sequences to be interpreted, and a single \ isn't well-formed
(note that you doubled the \ in your Perl example, too).
b=re.sub('$', '\\\\', a)
or
b=re.sub('$', chr(0x5C)+chr(0x5C), a)
works just fine, as does
b = a + chr(0x5C)
In all these cases,
print(b)
prints
abcd\
(with a single backslash at the end)
But simply typing "b" at the REPL produces
'abcd\\'
because prints the __repr__() of an object. Note that it also prints
single quotes, which are also not part of the string.
hp
--
_ | Peter J. Holzer | Story must make more sense than reality.
|_|_) | |
| | |
h...@hjp.at | -- Charles Stross, "Creative writing
__/ |
http://www.hjp.at/ | challenge!"