All Topics
All Topics
Technology
Technology
Design
Design
Programming
Programming
Science
Science
News
News
Gaming
Gaming
Entertainment
Entertainment
Business
Business
Finance
Finance
Sports
Sports
Health
Health
Food
Food
Travel
Travel
Art
Art
Music
Music
Books
Books
Education
Education
Politics
Politics
Personal
Personal
No algorithm. No AI slop. No ads. Just RSS. Pro-human. Indie writers. Real journalism. Open web. Chronological. Hand toasted.

Python 3 String Encoding Solution: Handling Mixed ASCII and UTF-8 Bytestrings

By

apitman

3mo ago· 2 min readen

Summary

The article discusses a Python programming challenge involving mixed string encodings (ASCII and UTF-8) in Python 3 environments. The author created a function to handle bytestrings that sometimes decode properly and sometimes don't, particularly when working with various human languages containing Unicode symbols. The solution involves trying to decode bytestrings and returning "bytes" if decoding fails, with an update mentioning the code is now available on GitHub with additional features including an input function.

Key quotes

· 3 pulled
Since Python3, I have been working with bytestrings that sometimes decode as ASCII or UTF strings ; the environment this lives in is mixed, as in some strings are expected to decode while some are are expected to not decode.
Displaying those string as-is is not convenient, as quite a few Unicode symbols will not render correctly : since I work with all sort of human languages these are frequent so I wrote a very short function that would try to decode my bytestring and return 'bytes' if it wouldn't decode.
UPDATE: Now on Github with more stuff including a neat input function
Snippet from the RSS feed
UPDATE: Now on Github with more stuff including a neat input function

You might also wanna read