💾 Archived View for kotobank.ch › ~merlin › python_quote.gmi captured on 2023-06-14 at 13:56:39. Gemini links have been rewritten to link to archived content
⬅️ Previous capture (2023-01-29)
-=-=-=-=-=-=-
If you were looking for a python equivalent of strconv.Quote or using json.dumps(str) to quote strings, then you are a clod - such as me. Another reminder that looking at the documentation of the standard library is very useful.
It never occurred to me that codecs (str.encode) are used not only for converting between text encodings like utf-8 ↔️ cp1251, but also for all sorts of formats like base64, bzip (sic!) and uu. In particular, the set of standard codecs includes 'unicode_escape', which escapes all characters that make a string unrepresentable as a Python literal in source code.
The only underwater rake - quotes are not taken into account, so the resulting string needs to be post-processed. But in general, this is a much less hacky (and probably faster) way to do "secured strings" than those mentioned at the beginning.
import codecs # Convert the operand using uuencode. codecs.encode(b'Alice\'s Adventures in Wonderland', 'uu') #> b"begin 666 <data>\n@06QI8V4G<R!!9'9E;G1U<F5S(&EN(%=O;F1E<FQA;F0 \n \nend\n" # Compress the operand using gzip. codecs.encode(b'Alice\'s Adventures in Wonderland', 'zlib') #> b'x\x9cs\xcc\xc9LNU/VpL)K\xcd+)-J-V\xc8\xccS\x08\xcf\xcfKI-\xcaI\xccK\x01\x00\xbe\x1f\x0b\xdf' # the Caesar-cypher encryption of the operand codecs.encode('Alice\'s Adventures in Wonderland', 'rot13') #> "Nyvpr'f Nqiragherf va Jbaqreynaq"