How to read space separated file in python?

Peter Otten __peter__ at web.de
Fri Nov 21 04:29:25 EST 2008


ganesh gajre wrote:

> Hi all,
> 
> I want to read file which is mapping file. Used in to map character from
> ttf to unicode.
> eg
> 
> Map file contain data in the following way:
> 
> 0 ०
> 1 १
> 2 २
> 3 ३
> 4 ४
> 5 ५
> 6 ६
> 7 ७
> 8 ८
> 9 ९
> 
> Like this. Please use any unicode editor to view the text if it not
> properly shown.
> 
> Now i want to read both the character separately like:
> 
> str[0]=0 and str2[0]=०
> 
> How can i do this?
> 
> please give me solution?

Read the file:

>>> import codecs
>>> pairs = [line.split() for line in codecs.open("ganesh.txt",
encoding="utf-8")]
>>> pairs[0]
[u'0', u'\u0966']

Create the conversion dictionary:

>>> trans = dict((ord(s), t) for s, t in pairs)

Do the translation: 

>>> print u"01109876".translate(trans)
०११०९८७६

You may have to use int(s) instead of ord(s) in your actual conversion code:

>>> trans = dict((int(s), t) for s, t in pairs)
>>> print u"\x00\x01\x09".translate(trans)
०१९

Peter



More information about the Python-list mailing list