3 ms·
Very well, now how do I put down that assumption? Is "read 100 bytes, utf8-decode them" right example?
by rini17 10y ago
Very well, now how do I put down that assumption? Is "read 100 bytes, utf8-decode them" right example?
- detaro 10y agoThe example probably does the truncation this way to just show the effect of decode() on the console, but if you really want a truncated version you should either decode everything and then truncate or use a StreamReader to only decode the first X characters. The docs probably should make that issue clear, so yes, the example there is badly choosen. Another real-life answer probably is "don't use urllib directly", since sadly the trend seems to go away from stdlib libraries for these things. E.g. Requests will take the encoding from the HTTP headers if available, and allows you to manually specify it as well if you want.