2011-07-18 52 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