[CentralOH] Removing Punctuation

pybokeh pybokeh at gmail.com
Tue Jan 21 00:17:07 CET 2014


Nice, didn't know about the str.translate() method.

But it is quite clear what my script is doing, whereas, to someone not 
familiar
with translate(), it isn't so obvious.  I'll take readability over 
esoteric Python methods,
unless maybe for performance reason, we should use the translate() method?

Anyways, thanks for sharing the str.translate() method!

BTW, here's the output of doing help(str.translate) if others happen to 
read this:

translate(...)
     S.translate(table) -> str
     
     Return a copy of the string S, where all characters have been mapped
     through the given translation table, which must be a mapping of
     Unicode ordinals to Unicode ordinals, strings, or None.
     Unmapped characters are left untouched. Characters mapped to None
     are deleted.



Regards,
Daniel

On 01/20/2014 05:56 PM, jep200404 at columbus.rr.com wrote:
> On Mon, 20 Jan 2014 16:09:56 -0500, jep200404 at columbus.rr.com wrote:
>
>> [1] See http://colug.net/python/dojo/20140117/word-count-example-rev2.ipynb
>        "for character in bunch_of_words:\n",
>        "    if character in string.punctuation:\n",
>        "        bunch_of_words = bunch_of_words.replace(character,\"\")\n",
>
> can be simplified as .translate(None, string.punctuation)
>
> as in http://colug.net/python/dojo/20140117/word-count-example-rev3.ipynb
> _______________________________________________
> CentralOH mailing list
> CentralOH at python.org
> https://mail.python.org/mailman/listinfo/centraloh



More information about the CentralOH mailing list