我需要帮助把所有的信息在一个JSON文件到一个结构化的输出。JSON是这样的:
[{"id":13076,"title":"Super Conference","coins":[{"id":"bitcoin","name":"Bitcoin","symbol":"BTC"},{"id":"cryptocurrencies","name":"Cryptocurrencies","symbol":"CRYPTO"},{"id":"ethereum","name":"Ethereum","symbol":"ETH"}],"date_event":"2018-07-13T00:00:00+01:00","created_date":"2018-05-18T00:11:59+01:00","description":"July 13, 2018 - July 15, 2018. Starting at 8 a.m to 8 p.m Location: 283 Duke Street West, Kitchener ON, Canada.","proof":"https:\/\/d32bfp67k1q0s7.cloudfront.net\/f7b766bc8fdbb39111ee949afcb2c56b.png","source":"https:\/\/blockchainsuperconference.com","is_hot":false,"vote_count":38,"positive_vote_count":32,"percentage":84,"categories":[{"id":5,"name":"Conference"}],"tip_symbol":null,"tip_adress":null,"twitter_account":null,"can_occur_before":false},{"id":13071,"title":"ETHGlobal Hackathon","coins":[{"id":"ethereum","name":"Ethereum","symbol":"ETH"}],"date_event":"2018-09-07T00:00:00+01:00","created_date":"2018-05-17T17:04:20+01:00","description":"Hackathon, workshops and talks in Berlin.","proof":"https:\/\/d32bfp67k1q0s7.cloudfront.net\/8d0c30eb00cbf791b74f4819f5fb097a.jpeg","source":"http:\/\/ethberlin.com\/","is_hot":false,"vote_count":25,"positive_vote_count":22,"percentage":88,"categories":[{"id":5,"name":"Conference"},{"id":6,"name":"Community Event"},{"id":7,"name":"Other"}],"tip_symbol":"LTC","tip_adress":"LKj8TgPzqDZMUxGRhL92yVJxMv8ZbZfr8E","twitter_account":"@Sokercat","can_occur_before":false},{"id":12872,"title":"Devcon4","coins":[{"id":"cryptocurrencies","name":"Cryptocurrencies","symbol":"CRYPTO"},{"id":"ethereum","name":"Ethereum","symbol":"ETH"}],"date_event":"2018-10-30T00:00:00+00:00","created_date":"2018-05-14T03:00:38+01:00","description":"Devcon\u2019s purpose-to serve the global community of Ethereum developers, designers and ecosystem-builders to connect, learn and share.","proof":"https:\/\/d32bfp67k1q0s7.cloudfront.net\/21f5a2e868ee750fe1c2b0af877339d4.png","source":"https:\/\/blog.ethereum.org\/2018\/05\/11\/devcon4-announcement\/","is_hot":false,"vote_count":28,"positive_vote_count":22,"percentage":79,"categories":[{"id":5,"name":"Conference"}],"tip_symbol":"ETH","tip_adress":"0x1Fff560EBCcCF3DDD7d9Fd77C00242dA5D62b271","twitter_account":"@neogeojr","can_occur_before":false},{"id":12675,"title":"Distributed 2018","coins":[{"id":"cryptocurrencies","name":"Cryptocurrencies","symbol":"CRYPTO"},{"id":"ethereum","name":"Ethereum","symbol":"ETH"},{"id":"poet","name":"Po.et","symbol":"POE"},{"id":"vechain","name":"VeChain","symbol":"VEN"}],"date_event":"2018-07-19T00:00:00+01:00","created_date":"2018-05-08T18:49:53+01:00","description":"Distributed 2018 occurs in San Francisco from July 19-20.","proof":"https:\/\/d32bfp67k1q0s7.cloudfront.net\/09e50e8a61f872c1a6d8891495636d4d.png","source":"http:\/\/2018.distributed.com\/","is_hot":false,"vote_count":102,"positive_vote_count":91,"percentage":89,"categories":[{"id":5,"name":"Conference"}],"tip_symbol":null,"tip_adress":null,"twitter_account":null,"can_occur_before":false}]我想要获取所有的date_event、描述和百分比,并像这样输出它(基于JSON中的信息量的多个条目):
Date: 2018-07-13 (not sure how to remove the stuff after T in 2018-07-13T00:00:00+01:00)
Description: Hackathon, workshops and talks in Berlin.
Percentage: 79我知道如何通过如下操作来获取json信息
for i in json:
date = i['date_event']但我不确定如何将所有的东西放在一起。
谢谢!
发布于 2018-06-05 06:16:14
使用json包,并确保JSON在使用引号的字符串中,例如:
import json
spam = '[{"id":13076,"title":"Super Conference","coins":[{"id":"bitcoin","name":"Bitcoin","symbol":"BTC"},{"id":"cryptocurrencies","name":"Cryptocurrencies","symbol":"CRYPTO"},{"id":"ethereum","name":"Ethereum","symbol":"ETH"}],"date_event":"2018-07-13T00:00:00+01:00","created_date":"2018-05-18T00:11:59+01:00","description":"July 13, 2018 - July 15, 2018. Starting at 8 a.m to 8 p.m Location: 283 Duke Street West, Kitchener ON, Canada.","proof":"https:\/\/d32bfp67k1q0s7.cloudfront.net\/f7b766bc8fdbb39111ee949afcb2c56b.png","source":"https:\/\/blockchainsuperconference.com","is_hot":false,"vote_count":38,"positive_vote_count":32,"percentage":84,"categories":[{"id":5,"name":"Conference"}],"tip_symbol":null,"tip_adress":null,"twitter_account":null,"can_occur_before":false},{"id":13071,"title":"ETHGlobal Hackathon","coins":[{"id":"ethereum","name":"Ethereum","symbol":"ETH"}],"date_event":"2018-09-07T00:00:00+01:00","created_date":"2018-05-17T17:04:20+01:00","description":"Hackathon, workshops and talks in Berlin.","proof":"https:\/\/d32bfp67k1q0s7.cloudfront.net\/8d0c30eb00cbf791b74f4819f5fb097a.jpeg","source":"http:\/\/ethberlin.com\/","is_hot":false,"vote_count":25,"positive_vote_count":22,"percentage":88,"categories":[{"id":5,"name":"Conference"},{"id":6,"name":"Community Event"},{"id":7,"name":"Other"}],"tip_symbol":"LTC","tip_adress":"LKj8TgPzqDZMUxGRhL92yVJxMv8ZbZfr8E","twitter_account":"@Sokercat","can_occur_before":false},{"id":12872,"title":"Devcon4","coins":[{"id":"cryptocurrencies","name":"Cryptocurrencies","symbol":"CRYPTO"},{"id":"ethereum","name":"Ethereum","symbol":"ETH"}],"date_event":"2018-10-30T00:00:00+00:00","created_date":"2018-05-14T03:00:38+01:00","description":"Devcon\u2019s purpose-to serve the global community of Ethereum developers, designers and ecosystem-builders to connect, learn and share.","proof":"https:\/\/d32bfp67k1q0s7.cloudfront.net\/21f5a2e868ee750fe1c2b0af877339d4.png","source":"https:\/\/blog.ethereum.org\/2018\/05\/11\/devcon4-announcement\/","is_hot":false,"vote_count":28,"positive_vote_count":22,"percentage":79,"categories":[{"id":5,"name":"Conference"}],"tip_symbol":"ETH","tip_adress":"0x1Fff560EBCcCF3DDD7d9Fd77C00242dA5D62b271","twitter_account":"@neogeojr","can_occur_before":false},{"id":12675,"title":"Distributed 2018","coins":[{"id":"cryptocurrencies","name":"Cryptocurrencies","symbol":"CRYPTO"},{"id":"ethereum","name":"Ethereum","symbol":"ETH"},{"id":"poet","name":"Po.et","symbol":"POE"},{"id":"vechain","name":"VeChain","symbol":"VEN"}],"date_event":"2018-07-19T00:00:00+01:00","created_date":"2018-05-08T18:49:53+01:00","description":"Distributed 2018 occurs in San Francisco from July 19-20.","proof":"https:\/\/d32bfp67k1q0s7.cloudfront.net\/09e50e8a61f872c1a6d8891495636d4d.png","source":"http:\/\/2018.distributed.com\/","is_hot":false,"vote_count":102,"positive_vote_count":91,"percentage":89,"categories":[{"id":5,"name":"Conference"}],"tip_symbol":null,"tip_adress":null,"twitter_account":null,"can_occur_before":false}]'
eggs = json.loads(spam)
for entry in eggs:
for key, value in entry.items():
print(key, value)编辑:您还可以使用内置的str函数,如下所示:
spam = str(JSON_stuff)发布于 2018-06-05 06:23:42
添加到@星期四即将到来的答案中:
像这样过滤你的输出:
for entry in eggs:
print 'Date:', entry['date_event'][:10]
print 'Description:', entry['description']
print 'Percentage:', entry['percentage']
print ''
# Date: 2018-09-07
# Description: Hackathon, workshops and talks in Berlin.
# Percentage: 88
#
# ...https://stackoverflow.com/questions/50689388
复制相似问题