user2973395
user2973395

Reputation: 37

coding (unicode) hex to octal

I read a text file, which has some characters like that '\260' (it means '°'), and then I add it to DB (sqlite3).

After that, I try to get the information from DB, but the sql-query will be built with '\xb0'(it means '°' too), because I get this information from XML file.

I try to replace hex characters with octal chracters: text = text.replace(r'\xb0', '\260') but it doesn't work, why? I cannot build correct sql-query.

Maybe there are some solutions for this problem e.g. encode, decode etc.

Upvotes: 1

Views: 289

Answers (1)

Martijn Pieters
Martijn Pieters

Reputation: 1122172

\260 is the same thing as \xb0:

>>> '\xb0'
'\xb0'
>>> '\260'
'\xb0'

You probably want to decode your input to unicode and insert that instead. If your data is encoded to Latin 1 then decode:

>>> print '\xb0'.decode('latin1')
°

sqlite3 can handle unicode just fine, and by decoding you make sure you are handling text values, not byte values, which can differ from codec to codec.

Upvotes: 3

Related Questions