views:

161

answers:

4

I'm working on a little script to help me learn the Japanese Kana (Hiragana/Katakana). In total, there are probably 100+ (+||-).

Basically, all it would do is take in the english version and convert it to the character.

ie. a = 'あ' which is 12354 in decimal

What I have so far is this:

hiraDict = { "a" : 12354, "i" : 12356 ...}

if __name__ == "__main__":
    if hiraDict.has_key(sys.argv[1]):
      print(unichr(hiraDict[sys.argv[1]]))

With 100+ characters, would this work OK or is there a better way to approach this?

+3  A: 

Put an encoding header as per PEP 263, then use the characters directly:

# -*- coding: utf-8 -*-
hiraDict = {'a': u'あ', 'i': u'い', ...}
Ignacio Vazquez-Abrams
That will help skip the conversion step. Thanks.
a51ts4
+1  A: 

From the docs:

has_key(key)

Test for the presence of key in the dictionary. has_key() is deprecated in favor of key in d.

So rewrite to

if sys.argv[1] in hiraDict:

danben
Oh, I didn't even notice. Thanks :)
a51ts4
+5  A: 

No need to check for presence, just get it and provide a default argument if you don't want an exception:

# -*- coding: utf-8 -*-
import sys
hiraDict = {'a': 'あ', 'i': 'い', }
print(hiraDict.get(sys.argv[1], None))

... and for python 2.x:

# -*- coding: utf-8 -*-
import sys
hiraDict = {'a': u'あ', 'i': u'い', }
print hiraDict.get(sys.argv[1], None)

[edit] I just noticed that you want to do this for a number of characters. The following will let you print out a series of characters given as arguments (python 3+):

# -*- coding: utf-8 -*-
import sys
hiraDict = {ord('a'): 'あ', ord('i'): 'い' }
text = " ".join(sys.argv[1:])
print(text.translate(hiraDict))
Will Hardy
Excellent. I should be looking at the docs more often. I am missing little things like this.
a51ts4
whrde, please add an `u` before the unicode text. (Or add a warning that this only works using Python-3.)
The first line allows all utf-8 characters to be used.
recursive
+1  A: 

Not only 100, this would work fine with 100k characters too.

So no need to worry about optimizing performance really, unless you will be using this routine thousands times per second, which you probably would not.

kibitzer