【问题标题】:How to show specific attribute from json file and write into another json file using Python?如何显示 json 文件中的特定属性并使用 Python 写入另一个 json 文件?
【发布时间】:2015-11-25 09:24:18
【问题描述】:

我遇到了一些关于从 json 文件中检索属性的问题。执行脚本后收到此错误消息

AttributeError: 'list' object has no attribute 'values'

我的代码

import json

githubusers_data_path = 'githubusers.json'

githubusers_file = open(githubusers_data_path, "r")
githubusers_string = ''.join(line for line in githubusers_file)
githubusers_string = '[{}]'.format(githubusers_string)
githubusers_data = json.loads(githubusers_string)

for val in githubusers_data.values():
    print val["login"]

然后我想要的是用这个理想的输出(必须有断线)重写现有文件(output.json)。

{"login": "crynobone"}
{"login": "syamilmj"}
{"login": "kamal"}
{"login": "datomnurdin"}
{"login": "ejamesc"}
{"login": "kagesenshi"}
{"login": "soggie"}
{"login": "aizatto"}
{"login": "mahmudahsan"}
{"login": "erikdubbelboer"}
...

原始数据

{
    "login": "datomnurdin",
    "id": 5416242,
    "avatar_url": "https://avatars.githubusercontent.com/u/5416242?v=3",
    "gravatar_id": "",
    "url": "https://api.github.com/users/datomnurdin",
    "html_url": "https://github.com/datomnurdin",
    "followers_url": "https://api.github.com/users/datomnurdin/followers",
    "following_url": "https://api.github.com/users/datomnurdin/following{/other_user}",
    "gists_url": "https://api.github.com/users/datomnurdin/gists{/gist_id}",
    "starred_url": "https://api.github.com/users/datomnurdin/starred{/owner}{/repo}",
    "subscriptions_url": "https://api.github.com/users/datomnurdin/subscriptions",
    "organizations_url": "https://api.github.com/users/datomnurdin/orgs",
    "repos_url": "https://api.github.com/users/datomnurdin/repos",
    "events_url": "https://api.github.com/users/datomnurdin/events{/privacy}",
    "received_events_url": "https://api.github.com/users/datomnurdin/received_events",
    "type": "User",
    "site_admin": false,
    "score": 1.0
}, {
    "login": "ejamesc",
    "id": 337175,
    "avatar_url": "https://avatars.githubusercontent.com/u/337175?v=3",
    "gravatar_id": "",
    "url": "https://api.github.com/users/ejamesc",
    "html_url": "https://github.com/ejamesc",
    "followers_url": "https://api.github.com/users/ejamesc/followers",
    "following_url": "https://api.github.com/users/ejamesc/following{/other_user}",
    "gists_url": "https://api.github.com/users/ejamesc/gists{/gist_id}",
    "starred_url": "https://api.github.com/users/ejamesc/starred{/owner}{/repo}",
    "subscriptions_url": "https://api.github.com/users/ejamesc/subscriptions",
    "organizations_url": "https://api.github.com/users/ejamesc/orgs",
    "repos_url": "https://api.github.com/users/ejamesc/repos",
    "events_url": "https://api.github.com/users/ejamesc/events{/privacy}",
    "received_events_url": "https://api.github.com/users/ejamesc/received_events",
    "type": "User",
    "site_admin": false,
    "score": 1.0
},...

【问题讨论】:

  • 请不要在给出答案的情况下编辑您的问题。如果您找到了解决方案,请发布另一个答案或编辑对您有帮助的答案。

标签: python json github github-api


【解决方案1】:
import json

githubusers_data_path = 'data.txt'

githubusers_file = open(githubusers_data_path, "r")
githubusers_string = ''.join(line for line in githubusers_file)
githubusers_string = '[{}]'.format(githubusers_string)
githubusers_data = json.loads(githubusers_string)
out = open( 'output.txt', 'w' )
for val in githubusers_data:
    dict = {}
    dict['login'] = val["login"]
    out.write( json.dumps(dict) + '\n' )
out.close()

输出:

{"login": "datomnurdin"}
{"login": "ejamesc"}
...

【讨论】:

  • 我支持你,因为答案是正确的。但是写入文件呢?
【解决方案2】:

您可以使用以下代码实现此目的:

githubusers_data_path = 'githubusers.json'

githubusers_file = open(githubusers_data_path, "r")
githubusers_string = ''.join(line for line in githubusers_file)
githubusers_string = '[{}]'.format(githubusers_string)
githubusers_data = json.loads(githubusers_string)

for val in githubusers_data:
    print '{"%s": "%s"}' % ("login", val["login"])

【讨论】:

  • 我支持你,因为答案是正确的。但是写入文件呢?
【解决方案3】:
logins = "\n".join(map(lambda u: '{"login": "%s"}' % u['login'], githubusers_data))
open("logins.txt", "w").write(logins)

【讨论】:

    猜你喜欢
    • 2019-03-24
    • 1970-01-01
    • 1970-01-01
    • 2020-03-19
    • 2021-06-08
    • 2022-11-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多