2011-07-18 54 views
2

Noob here。我有大量的json文件,每個文件都是用不同語言編寫的一系列博文。鍵值對是關於帖子的元數據,例如, 「{'author':'John Smith','translator':'Jane Doe'}。我想要做的是將它轉換爲Python字典,然後提取值,以便我有一個所有作者和翻譯者的列表在所有的職位。JSON to python字典:打印值

for lang in languages: 
    f = 'posts-' + lang + '.json' 
    file = codecs.open(f, 'rt', 'utf-8') 
    line = string.strip(file.next()) 
    postAuthor[lang] = [] 
    postTranslator[lang]=[] 

    while (line): 
     data = json.loads(line) 
     print data['author'] 
     print data['translator'] 

當這個方法我試過,我不斷收到對翻譯的關鍵錯誤,我不知道爲什麼。我從來沒有使用JSON模塊工作過,所以我嘗試了更復雜方法來看看發生了什麼:。

postAuthor[lang].append(data['author']) 
    for translator in data.keys(): 
     if not data.has_key('translator'): 
      postTranslator[lang] = "" 
     postTranslator[lang] = data['translator'] 

它使返回的字符串不具有附加功能的錯誤這似乎是一個簡單的任務,我不知道我在做什麼錯

+0

您是否嘗試過'打印data'在'json.loads'後的第一個版本?它輸出什麼? –

+0

它以鍵值格式打印博客文章的全部內容,所有元數據等。 – MRose429

+0

你可以粘貼一個數據結構的樣本嗎?第一個例子中的代碼沒有任何問題會導致您收到的錯誤,除非數據不完整。也許可以嘗試打印'data.keys()'並確保'翻譯器'中沒有空格或其他內容。你也可以嘗試data.get('translator','')',如果翻譯器沒有設置爲文章,它將填充一個空白字符串。 –

回答

2

看看這對你的作品:

import json 

# you have lots of "posts", so let's assume 
# you've stored them in some list. We'll use 
# the example text you gave as one of the entries 
# in said list 

posts = ["{'author':'John Smith', 'translator':'Jane Doe'}"] 

# strictly speaking, the single-quotes in your example isn't 
# valid json, so you'll want to switch the single-quotes 
# out to double-quotes, you can verify this with something 
# like http://jsonlint.com/ 
# luckily, you can easily swap out all the quotes programmatically 

# so let's loop through the posts, and store the authors and translators 
# in two lists 
authors = [] 
translators = [] 

for post in posts: 
    double_quotes_post = post.replace("'", '"') 
    json_data = json.loads(double_quotes_post) 

    author = json_data.get('author', None) 
    translator = json_data.get('translator', None) 

    if author: authors.append(author) 
    if translator: translators.append(translator) 

# and there you have it, a list of authors and translators