Fix Python – Write to UTF-8 file in Python

I’m really confused with the codecs.open function. When I do:
file = codecs.open(“temp”, “w”, “utf-8”)
file.write(codecs.BOM_UTF8)
file.close()

It gives me the error

UnicodeDecodeError: ‘ascii’ codec can’t decode byte 0xef in position
0: ordinal not in range(128)

If I do:
file = open(“temp”, “w”)
file.write(codecs.BOM_UTF8)
file.close()

It w….

Fix Python – u’\ufeff’ in Python string

I got an error with the following exception message:
UnicodeEncodeError: ‘ascii’ codec can’t encode character u’\ufeff’ in
position 155: ordinal not in range(128)

Not sure what u’\ufeff’ is, it shows up when I’m web scraping. How can I remedy the situation? The .replace() string method doesn’t work on it.
….

Fix Python – error UnicodeDecodeError: ‘utf-8’ codec can’t decode byte 0xff in position 0: invalid start byte

https://github.com/affinelayer/pix2pix-tensorflow/tree/master/tools
An error occurred when compiling “process.py” on the above site.
python tools/process.py –input_dir data — operation resize –outp
ut_dir data2/resize
data/0.jpg -> data2/resize/0.png

Traceback (most recent call last):
File “tools/process.py”, line 235, in
….

Fix Python – Url decode UTF-8 in Python

I have spent plenty of time as far as I am newbie in Python.
How could I ever decode such a URL:
example.com?title=%D0%BF%D1%80%D0%B0%D0%B2%D0%BE%D0%B2%D0%B0%D1%8F+%D0%B7%D0%B0%D1%89%D0%B8%D1%82%D0%B0

to this one in python 2.7: example.com?title==правовая+защита
url=urllib.unquote(url.encode(“utf8”)) is returning something very ugly.
Still no so….

Fix Python – Saving utf-8 texts with json.dumps as UTF8, not as \u escape sequence

Sample code:
>>> import json
>>> json_string = json.dumps(“ברי צקלה”)
>>> print(json_string)
“\u05d1\u05e8\u05d9 \u05e6\u05e7\u05dc\u05d4”

The problem: it’s not human readable. My (smart) users want to verify or even edit text files with JSON dumps (and I’d rather not use XML).
Is there a way to serialize objects into UTF-8 JSON strings (instead ….