Casey Bralla via plug on 29 Jun 2023 03:56:19 -0700


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

Re: [PLUG] Help With Python String Replacement


I never knew about the "raw string" in python.  Thanks for the help!  And yes, it can be "damned confusing" :)

On 6/29/2023 1:42 AM, Lynn Bradshaw wrote:
A little search led me here:

https://sceweb.sce.uhcl.edu/helm/WEBPAGE-Python/documentation/howto/regex/node8.html

After messing about with IPython for a little bit, I came up with the following helpful demonstration using Python "raw" strings that you can see here:

https://i.imgur.com/pQsIjYY.png

It's still damned confusing. Notice there is a difference between how IPython displays string literals that it's just given and how the print function displays them, which is as intended, and what you will get in standard output or error or in an output file. (I tested the latter claim just now and can confirm it is true). Oh, also for anyone who might want text for search purposes, it's small enough to include here I think without Pastebin or the like:

In [53]: test = r'\\ \,'
In [54]: print(test)
\\ \,
In [55]: print(test.replace(r'\\', '\\'))
\ \,
In [56]: test = test.replace(r'\\', '\\')
In [57]: print(test.replace(r'\,', ','))
\ ,
In [58]: test = test.replace(r'\,', ',')
In [59]: print(test)
\ ,

One final style point, just in case: "StringVariable" was formatted with each word capitalized. Typically you're only going to want to use that for user-defined classes (including, say, instances of collections.namedtuple and exceptions). The Google Python style guide offers very succinct advice on what you will usually want to make communication with other Pythonistas (and yourself) easy:

https://google.github.io/styleguide/pyguide.html#316-naming

So, in this instance, you would want to use "snake case" and spell it out as "string_variable", using underscores between lowercase words.

Hope this all helps.

On Wed, Jun 28, 2023 at 1:47 PM Casey Bralla via plug <plug@lists.phillylinux.org> wrote:
This should be a no-brainer, but I guess I fit in the category of "no
brain" :)

My python script is looking through a text file and wants to fix some
character strings that contain escape characters.

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("\,",",")


Can somebody help me with this code snippet?


TIA!

Casey

--
LEGAL NOTICE: This eMail contains private, personal, and/or privileged
information and is only for the intended recipient(s). In fact, you
really should consider yourself honored to even be cc'd on this
tremendously important communication. The author spent literally
seconds composing this magnificent opus of rational thought and
deductive logic. Unfortunately, it has probably been based on
inaccurate data, which really stinks because this eMail would have been
truly awesome! If you have received this eMail in error, we
respectfully DEMAND that you immediately delete it and inform the sender
that you have received it in error. Then, just to be safe, you should
reformat your hard drive, shave your head, renounce all material
possessions (which are really controlling your life anyway), and join an
end-of-times cult somewhere. Once there, you must reconsider all the
terrible choices you've made in your life, and promise never to confuse
"sex" with "gender" again. Of course, this assumes you have already
come to terms with your inherent whiteness, AND that you have learned
the lyrics to The Internationale. "Arise, wretched of the earth! Arise,
convicts of hunger..."
(https://en.wikipedia.org/wiki/The_Internationale) We sincerely hope
you are able to get your medication stabilized and no longer have
that recurring dream where you're alone in a large crowd, standing
naked in a vat of chocolate Yoo-hoo. BTW, Yoo-hoo really is an
underrated beverage. It’s chocolatey, yet suprisingly refreshing. Pick
up a 6-pack today, and tell your friends!

___________________________________________________________________________
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
-- 
LEGAL NOTICE:  This eMail contains private, personal, and/or privileged
information and is only for the intended recipient(s).  In fact, you
really should consider yourself honored to even be cc'd on this
tremendously important communication.  The author spent literally
seconds composing this magnificent opus of rational thought and
deductive logic.  Unfortunately, it has probably been based on
inaccurate data, which really stinks because this eMail would have been
truly awesome!  If you have received this eMail in error, we
respectfully DEMAND that you immediately delete it and inform the sender
that you have received it in error.  Then, just to be safe, you should
reformat your hard drive, shave your head, renounce all material
possessions (which are really controlling your life anyway), and join an
end-of-times cult somewhere.  Once there, you must reconsider all the
terrible choices you've made in your life, and promise never to confuse
"sex" with "gender" again.  Of course, this assumes you have already
come to terms with your inherent whiteness, AND that you have learned
the lyrics to The Internationale. "Arise, wretched of the earth!  Arise,
convicts of hunger..."
(https://en.wikipedia.org/wiki/The_Internationale)  We sincerely hope 
you are able to get your medication stabilized and no longer have 
that recurring dream where you're alone in a large crowd, standing 
naked in a vat of chocolate Yoo-hoo.  BTW, Yoo-hoo really is an 
underrated beverage.  It’s chocolatey, yet suprisingly refreshing. Pick
up a 6-pack today, and tell your friends!
___________________________________________________________________________
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