character-encoding

What is the difference between encoding utf-8 and utf8 in Python 3.5?

What is the difference between encoding utf-8 and utf8 in Python 3.5? Question: What is the difference between encoding utf-8 and utf8 (if there is any)? Given the following example: u = u’€’ print(‘utf-8’, u.encode(‘utf-8’)) print(‘utf8 ‘, u.encode(‘utf8′)) It produces the following output: utf-8 b’xe2x82xac’ utf8 b’xe2x82xac’ Asked By: bastelflp || Source Answers: There’s no …

Total answers: 2

Wrap an open stream with io.TextIOWrapper

Wrap an open stream with io.TextIOWrapper Question: How can I wrap an open binary stream – a Python 2 file, a Python 3 io.BufferedReader, an io.BytesIO – in an io.TextIOWrapper? I’m trying to write code that will work unchanged: Running on Python 2. Running on Python 3. With binary streams generated from the standard library …

Total answers: 6

'utf8' codec can't decode byte 0xf3

'utf8' codec can't decode byte 0xf3 Question: I am using python 2.7 to read a JSON file. My code is: import json from json import JSONDecoder import os path = os.path.dirname(os.path.abspath(__file__))+’/json’ print path for root, dirs, files in os.walk(os.path.dirname(path+’/json’)): for f in files: if f.lower().endswith((".json")): fp=open(root + ‘/’+f) data = fp.read() print data.decode(‘utf-8’) But I …

Total answers: 1

How to open html file that contains Unicode characters?

How to open html file that contains Unicode characters? Question: I have html file called test.html it has one word בדיקה. I open the test.html and print it’s content using this block of code: file = open(“test.html”, “r”) print file.read() but it prints ??????, why this happened and how could I fix it? BTW. when …

Total answers: 8

How to fix broken utf-8 encoding in Python?

How to fix broken utf-8 encoding in Python? Question: My string is Niệm Bồ Tát (Thiá»n sÆ° Nhất Hạnh) and I want to decode it to Niệm Bồ Tát (Thiền sư Nhất Hạnh). I see in that site can do that http://www.enderminh.com/minh/utf8-to-unicode-converter.aspx and I start to try by Python mystr = ’09. Bát Nhã Tâm …

Total answers: 4

Python easy way to set default encoding for opening files in text mode?

Python easy way to set default encoding for opening files in text mode? Question: Is there an easy and cross-platform way to set default encoding for opening files (text mode) in Python, so you don’t have to write open(filename, ‘r’, encoding=’utf-8′) each time and can simply write open(filename, ‘r’) ? Asked By: Bob || Source …

Total answers: 4

Using utf-8 characters in a Jinja2 template

Using utf-8 characters in a Jinja2 template Question: I’m trying to use utf-8 characters when rendering a template with Jinja2. Here is how my template looks like: <!DOCTYPE HTML> <html manifest=”” lang=”en-US”> <head> <meta charset=”UTF-8″> <title>{{title}}</title> … The title variable is set something like this: index_variables = {‘title’:”} index_variables[‘title’] = myvar.encode(“utf8”) template = env.get_template(‘index.html’) index_file …

Total answers: 3

"for line in…" results in UnicodeDecodeError: 'utf-8' codec can't decode byte

"for line in…" results in UnicodeDecodeError: 'utf-8' codec can't decode byte Question: Here is my code, for line in open(‘u.item’): # Read each line Whenever I run this code it gives the following error: UnicodeDecodeError: ‘utf-8’ codec can’t decode byte 0xe9 in position 2892: invalid continuation byte I tried to solve this and add an …

Total answers: 20

Convert an int value to unicode

Convert an int value to unicode Question: I am using pyserial and need to send some values less than 255. If I send the int itself the the ascii value of the int gets sent. So now I am converting the int into a unicode value and sending it through the serial port. unichr(numlessthan255); However …

Total answers: 4