Illia Olenchenko
Illia Olenchenko

Reputation: 128

Python project strings migrating from ascii to unicode

Ive got some lazy trouble about pythons strings.
I have a project with python 2.x and all strings we have there are 'blabla'.
Now we want to move this strings to unicode without taking extra libraries like __future__ or moving to python 3 or using sys.setdefaultencoding.
And i have to click this all through project to change '' to u''. But not all strings i need to change, for example fields of object i do not want to change:
obj = {'field': field}

A question: is there a way to make it automatic? And i have stacked with a next problem my regex [^u]([\'][^\'\"]*[\']) catches ' ' ' ' middle section which are not a string.

For now i have next replacements: (\'.*\') --> u$1

Upvotes: 0

Views: 98

Answers (1)

jfs
jfs

Reputation: 414835

is there a way to make it automatic?

If you mean -- is there a program that may decide what type of string (Unicode (u''), bytestring (b''), or native ('')) should be used in a specific place in an arbitrary program -- then no: there is no such program -- you should inspect each and every case very carefully. See Text versus binary data.

Upvotes: 1

Related Questions