iso-8859-1

Can you safely read utf8 and latin1 files with a naïve try-except block?

Can you safely read utf8 and latin1 files with a naïve try-except block? Question: I believe that any valid latin1 character will either be interpreted correctly by Python’s utf8 encoder or throw an error. I, therefore, claim that if you work with only either utf8 files or latin1 files, you can safely write the following …

Total answers: 3

UTF-8 to ISO-8859-1 encoding: replace special characters with closest equivalent

UTF-8 to ISO-8859-1 encoding: replace special characters with closest equivalent Question: Does anyone know of Python libraries that allows you to convert a UTF-8 string to ISO-8859-1 encoding in a smart way? By smart, I mean replacing characters like “–” by “-” or so. And for the many characters for which an equivalent really cannot …

Total answers: 4

convert em-dash to hyphen in python

convert em-dash to hyphen in python Question: I’m converting csv files into python Dataframe. And in the original file, one of the column has characters em-dash. I want it replaced by hyphen “-“. Partial original file from csv: NoDemande NoUsager Sens IdVehicule NoConduteur HeureDebutTrajet HeureArriveeSurSite HeureEffective’ 42192001801 42192002715 — 157Véh 42192000153 … 42192000003 42192002021 + …

Total answers: 2

Convert bytes data inside a string to a true bytes object

Convert bytes data inside a string to a true bytes object Question: In Python 3, I have a string like the following: mystr = “x00x00x01x01x80x02xc0x02x00” This string was read from a file and it is the bytes representation of some text. To be clear, this is a unicode string, not a bytes object. I need …

Total answers: 1

'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