brent saner via plug on 28 Jun 2023 11:33:51 -0700


[Date Prev] [Date Next] [Thread Prev] [Thread Next] [Date Index] [Thread Index]

Re: [PLUG] Help With Python String Replacement


On Wed, Jun 28, 2023 at 1:47 PM Casey Bralla via plug <plug@lists.phillylinux.org> wrote:
(SNIP)
I want to replace some double-slashes "\\" with single slashes "\".  I
also want to replace a slash-comma "\,"with a comma ","

But the python code is getting confused by the slashes as escape
characters.   Here's my code.

StringVariable.replace("\\","\")

StringVariable.replace("\,",",")

The easiest way is you need to "escape the escape".

.replace('\\\\', '\\')

But you can also do fun things like (ref):

.replace('\x5c\x5c', '\x5c')
___________________________________________________________________________
Philadelphia Linux Users Group         --        http://www.phillylinux.org
Announcements - http://lists.phillylinux.org/mailman/listinfo/plug-announce
General Discussion  --   http://lists.phillylinux.org/mailman/listinfo/plug