Site Sponsors:
ASCII Strings in Python 3 
Since Python decided to convert to Unicode, the default string encoding is no longer ASCII.

In as much as the vast majority of the world is still working with ASCII text however, lots of people are being frustrated.

In an effort to stem the frustration, allow me to share an example. Written in Python3, feel free to use this little ditty to inspire-power your own (whatever) to ASCII filtering & conversions:

def mk_printable(str_data):
result = bytearray(str_data, "utf-8")
for ss, code in enumerate(result):
if code < 32:
result[ss] = 0x20
if code > 126:
result[ss] = 0x20
return str(result, "utf-8")

In addition to converting a "whatever" string to UTF-8, note that the above routine will also map non-ascii encodings to an ASCII 'SPACE' character (0x20 in the above.)

We also leave the final conversion to UTF8 to Python's str() built-in function.

Enjoy the journey!



Add Comment
Comments are not available for this entry.