我需要帮助将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,description和百分比并将其输出(根据JSON中的信息量多个entires):
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']
但我不确定如何把所有东西放在一起以便将它们组合在一起。
谢谢!
答案 0 :(得分:0)
使用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)
答案 1 :(得分:0)
添加到@Thursdays即将回答:
按如下方式过滤输出:
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
#
# ...
答案 2 :(得分:0)
如果您想以特定顺序显示条目,我建议您将日期字符串转换为 datetime 格式。否则,只需在迭代JSON对象时拆分字符串。
from datetime import datetime
for i in json:
date = i['date_event']
date = datetime.strptime(date.split(date[10])[0], '%Y-%m-%d')
print "Date: " + str(date.year) + "-" + str(date.month) + "-" + str(date.day)
print "Description: ", i['description']
print "Percentage: ", i['percentage']