Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6317885
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T15:34:56+00:00 2026-05-24T15:34:56+00:00

I have two python dictionaries containing information about japanese words and characters: vocabDic :

  • 0

I have two python dictionaries containing information about japanese words and characters:

  1. vocabDic : contains vocabulary, key: word, value: dictionary with information about it
  2. kanjiDic : contains kanji ( single japanese character ), key: kanji, value: dictionary with information about it

    Now I would like to iterate through each character of each word in the vocabDic and look up this character in the kanji dictionary. My goal is to create a csv file which I can then import into a database as join table for vocabulary and kanji.
    My Python version is 2.6
    My code is as following:

    kanjiVocabJoinWriter = csv.writer(open('kanjiVocabJoin.csv', 'wb'), delimiter=',', quotechar='|', quoting=csv.QUOTE_MINIMAL)
    kanjiVocabJoinCount = 1
    
    #loop through dictionary
    for key, val in vocabDic.iteritems():
        if val['lang'] is 'jpn': # only check japanese words
            vocab = val['text']
            print vocab
            # loop through vocab string
            for v in vocab:
                 test = kanjiDic.get(v)
                 print v
                 print test
                 if test is not None:
                    print str(kanjiVocabJoinCount)+','+str(test['id'])+','+str(val['id'])
                    kanjiVocabJoinWriter([str(kanjiVocabJoinCount),str(test['id']),str(val['id'])])
                    kanjiVocabJoinCount = kanjiVocabJoinCount+1
    

If I print the variables to the command line, I get:
vocab : works, prints in japanese
v ( one character of the vocab in the for loop ) : �
test ( character looked up in the kanjiDic ) : None

To me it seems like the for loop messes the encoding up.
I tried various functions ( decode, encode.. ) but no luck so far.
Any ideas on how I could get this working?
Help would be very much appreciated.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-24T15:34:56+00:00Added an answer on May 24, 2026 at 3:34 pm

    From your description of the problem, it sounds like vocab is an encoded str object, not a unicode object.

    For concreteness, suppose vocab equals u'債務の天井' encoded in utf-8:

    In [42]: v=u'債務の天井'
    In [43]: vocab=v.encode('utf-8')   # val['text']
    Out[43]: '\xe5\x82\xb5\xe5\x8b\x99\xe3\x81\xae\xe5\xa4\xa9\xe4\xba\x95'
    

    If you loop over the encoded str object, you get one byte at a time: \xe5, then \x82, then \xb5, etc.

    However if you loop over the unicode object, you’d get one unicode character at a time:

    In [45]: for v in u'債務の天井':
       ....:     print(v)    
    債
    務
    の
    天
    井
    

    Note that the first unicode character, encoded in utf-8, is 3 bytes:

    In [49]: u'債'.encode('utf-8')
    Out[49]: '\xe5\x82\xb5'
    

    That’s why looping over the bytes, printing one byte at a time, (e.g. print \xe5) fails to print a recognizable character.

    So it looks like you need to decode your str objects and work with unicode objects. You didn’t mention what encoding you are using for your str objects. If it is utf-8, then you’d decode it like this:

    vocab=val['text'].decode('utf-8')
    

    If you are not sure what encoding val['text'] is in, post the output of

    print(repr(vocab))
    

    and maybe we can guess the encoding.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

If I have two dictionaries I'd like to combine in Python, i.e. a =
Possible Duplicate: python dict.add_by_value(dict_2) ? My input is two dictionaries that have string keys
I have two Python lists of dictionaries, entries9 and entries10 . I want to
I have two String.printable mysteries in the one question. First, in Python 2.6: >>>
I have two lists of dictionaries list1 = [ {..}, {..}, ..] list2 =
Im new two python and am trying to grow a dictionary of dictionaries. I
I have a problem with combining or calculating common/equal part of these two dictionaries.
I have a Python class C which should have two pseudo- dict s a
I have two python scripts. First one is just a script waiting for user
I have two python scripts that have to run simultaneously because they interact with

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.