2 ms·
The problem are external APIs returning whatever they want no matter what they should return. The world is messy. Example, I just had to write this return u
by pmontra 7y ago
The problem are external APIs returning whatever they want no matter what they should return. The world is messy.
Example, I just had to write this
return urllib.request
.urlopen(url, timeout=60)
.read()
.decode("utf-8", errors="backslashreplace")
(probably not valid code because of the newlines but you'll forgive me)
Then I use that string in a regexp, etc.
This is the only language where I have to explicitly deal with encodings at such low level. I don't feel like I want to use it for my pet projects.
- takeda 7y agoWhy is that bad? The result returned from an URL is always binary. In certain situations it could be text but it doesn't have to be. If the result was an image and you would want to convert the data to image, if it was sound file same, you should think of text as another distinguished type. Of course urllib could have method text() that would do such conversion, but then urllib is not requests. It never was user friendly. Edit: personally I use aiohttp, the interface is much nicer: https://aiohttp.readthedocs.io/en/stable/client_reference.html#aiohttp.ClientResponse.text https://aiohttp.readthedocs.io/en/stable/client_reference.ht... if I can't use asyncio then would use requests.