0

I have recently started learning Python and have got stuck in a small project I was trying. I have an array which contains data for my project, I wanted to link this by using the code.

    >>> keys = ['a', 'b', 'c']
    >>> values = [1, 2, 3]
    >>> dictionary = dict(zip(keys, values))

But for my project I need Japanese characters in my values array. Is there a method where I can have my Japanese characters in the Array? Also if I were to type the words in using unicode, how would I be able to display the words equivalent to the unicode in the dictionary For example:

    print(u'\4096')

Would work but if I were to print the entire dictionary as

    print (dictionary)

It wouldn't display my Japanese Characters. How would I be able to get around this problem?

Extra:

Another Problem is that I have my first array instead as a list as it was required to store information put together, is there an alternative solution?

    dictionary = dict(zip(file_content,japanese))
    TypeError: unhashable type: 'list'
SiHa
  • 7,830
  • 13
  • 34
  • 43
  • http://stackoverflow.com/questions/14682933/chinese-and-japanese-character-support-in-python – AlvaroP Sep 21 '16 at 12:39
  • As for your extra, use a tuple. The keys of a dictionary should be immutable. A tuple is immutable, whereas a list is not. Which is what the error message says. – Elan Sep 21 '16 at 12:40
  • @AlvaroP Thank you for the answer but I have already looked through that post, it still does not help me with my problem. For example I use IDLE python 3.5.3 and when printing unicode for Japanese characters I must have it typed out for some odd reason. I cannot take it out of the list and then print it. – okuyama kento Sep 21 '16 at 12:42
  • @Elan Thanks for the help concerning my Extra! If you have any ideas for my main problem answers would be appriciated :) – okuyama kento Sep 21 '16 at 12:43

1 Answers1

0

There are two major flavours of Python - Python2 and Python3, and one of the differences between them is how they treat Unicode. As a rule-of-thumb, you should be using Python3, because it is a much better language and getting better over time. Most of the bigger libraries support it.

In Python3, this should just work:

keys = ['a', 'b', 'c']
values = ['á', 'b', 'ç']
dictionary = dict(zip(keys, values))
print(dictionary) # {'a': 'á', 'b': 'b', 'c': 'ç'}

In Python2, the same thing should work, but you should be using "Unicode" strings, something like this:

keys = ['a', 'b', 'c']
values = [u'á', u'b', u'ç']
dictionary = dict(zip(keys, values))
print(dictionary) # {'a': u'\xe1', 'c': u'\xe7', 'b': u'b'}

The strings properly store the unicode contents you want, they are just not printed nicely, due to the naivité of the print function. But if you want to print a key, for example, it will figure it out and do the right thing.

You will always have to take care of properly encoding the strings when you're writing output to files or other types of streams.

This is a pretty good introduction to the topic of Python and strings.

Horia Coman
  • 8,681
  • 2
  • 23
  • 25
  • I would like to ask what if my Keys values contained arrays within arrays, I wouldn't be able to use dictionaries would I? – okuyama kento Sep 21 '16 at 12:47
  • You should be using tuples in that case, rather than arrays/lists. Keys can't be mutable, and tuples are immutable collections. You can make a tuple from a list with tuple(some_list). – Horia Coman Sep 21 '16 at 12:50
  • How would I put a Array Containing Characters like ["こんにちは"] with keys like [['hello', 'This is commonly used when greeting someone. It would not be used as a good morning but a hello when entering houses or when meeting someone for the first time', 'konnichiha']]? – okuyama kento Sep 21 '16 at 12:55