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.fl...@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 :-) > It is installable by pip. > Perl has no problems with handling backslashes. > > https://pypi.org/project/perl/ > > What you need is something like this: > > mgogala@umajor:~$ perl -e '$a="abcd";$a=~s/$/\\/; print "$a\n";' > abcd\ > > Python has a problem with backslashes: > > Python 3.8.5 (default, Jul 28 2020, 12:59:40) > [GCC 9.3.0] on linux > Type "help", "copyright", "credits" or "license" for more information. > >>> import re > >>> a="abcd" > >>> b=re.sub('$',chr(28),a) > >>> b > 'abcd\x1c' > > >>> b=re.sub('$',chr(0x41),a) > >>> b > 'abcdA' > >>> b=re.sub('$','Z',a) > >>> b > 'abcdZ' > >>> b=re.sub('$',chr(0x1C),a) > >>> b > 'abcd\x1c' > >>> > > Any other character is shown as it should be, except backslash. 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!"
signature.asc
Description: PGP signature
-- https://mail.python.org/mailman/listinfo/python-list