打开JSON文件Python [重复]

Posted

技术标签:

【中文标题】打开JSON文件Python [重复]【英文标题】:Opening JSON file Python [duplicate] 【发布时间】:2016-07-20 04:38:18 【问题描述】:

我正在尝试用 Python 读取 JSON 文件,不管你信不信,我不知道从哪里开始。这是我名为 AEE_2007.json 的 JSON 文件的样子:

"date": "October 30, 2007 Tuesday", "body": "For those of us who have been around Aberdeen for a while, your question \"What now for the oil industry? (Evening Express, October 26) had a touch of deja vu about it. That same question has been asked almost since the day the first drop of oil was pumped out of the North Sea. In the past 30 years we have seen a constant cycle of ups and downs, booms and busts in this industry. I can predict what  will  happen next. There will be a period of worry and uncertainty during which there will be a scrabble to find something to keep the local economy buoyant  when the oil is gone. Then an upturn will see more jobs and investment in oil, everyone will breathe a sigh of relief  and the quest to diversify will go  on the back burner. That will be our  downfall. Major industries  are prone to collapse. Look at our nation's   defunct shipyards and extinct coal and steel  industries. That's why it is vital we don't just panic during downturns, but start planning sensibly for our future. Our civic and business leaders need to be constantly looking for something  to secure our prosperity    -  be it tourism, technology, bio-science or other emerging industries. We need to do it now while we are economically strong rather than waiting to see what happens when the oil roller coaster hits the buffers. N JonesEllon ", "title": "Plan now for future after oil industry dies"
"date": "September 4, 2007 Tuesday", "body": "Westminster was to issue a \"hands-off\" warning today to the SNP administration at Holyrood over North Sea oil. Scotland Office minister David Cairns was to  tell Offshore Europe   the UK Government has no intention of devolving control over oil and gas to the Scottish Government. Mr Cairns was to say: The interests of both Scotland and the UK are best served through continued economic union and the benefits which accompany a UK-wide approach. Our thinking on this issue is therefore unequivocal  -  introducing needless uncertainty into an \u00a311 billion industry which supports half a million jobs is not an option for the UK Government. Before the Holyrood election, the SNP pledged to make it an  priority to begin discussions with Westminster on options for the transfer of responsibility for our oil and gas resources to the Scottish Parliament. But First Minister Alex Salmond was today expected to dismiss the attack by Mr Cairns. He was due to say at the opening of the conference that he was not going to be put off by the knee-jerk negativity of the junior minister from the Scotland Office. Mr Salmond was due to deliver a positive plea to get revolutionary carbon-capture proposals for Peterhead back on track. He was to describe Mr Cairns' remarks as narrow and negative. Mr Cairns was to tell the  conference at Aberdeen Exhibition and Conference Centre that North Sea oil is a UK resource and that the UK Government had no intention of devolving oil and gas powers to Holyrood for a vast array of sound reasons. Comment, Page 6 ", "title": "Salmond to hit back at 'hands off oil' warning"
"date": "June 22, 2007 Friday", "body": "Everyone knows that our city needs this road and needs it built on time and on budget. The alternative is unthinkable. It would mean decades more gridlock and delays. The result would be endless frustration for drivers and a block on economic growth and future prosperity for our city. Most of us were certain that the arguments for this bypass were so compelling as to be able to sweep away any of the objections that have been raised. Yet now there is a cloud of doubt hanging over the whole project because the new administration of the Scottish Executive wants a review of its costs. That is both surprising and alarming given the level of support from all the major parties for  this project with the exception of the car-hating Greens. This uncertainty is doing no one any favours. So why can't the transport minister Stewart Stevenson, pictured,  come right out and say that the Scottish Executive is fully committed  - no   buts and maybes -  to building our bypass? Unless, as Richard Baker MSP, suggests projects like our bypass are set to be sidelined. A EllisNorthfieldAberdeen ", "title": "End the doubt over our city's vital bypass"
"date": "June 8, 2007 Friday", "body": "The largest nature conservation charity in Scotland has lodged objections against Donald Trump's \u00a31 billion golf development. The Scottish Wildlife Trust  is concerned  the golf course will destroy a natural process essential for maintaining the sand dunes on the Menie Estate, Balmedie. The American tycoon wants to build two 18-hole championship courses, a five-star hotel, a golf academy and additional amenities on the 800-acre site. But his ambitious plans, which are predicted to pump millions in the North-east economy, have met with stiff opposition from nature groups. The RSPB and Scottish Natural Heritage have already lodged formal objections with Aberdeenshire Council and now the Scottish Wildlife Trust has joined them. Planning co-ordinator Paul Gallagher said: The application maintains an unacceptable level of impact on habitats and uncertainty remains in relation to the effect on species. The process of stopping sand from getting on the golf course would destroy the natural process which keeps the sand moving. Trump Golf Links Scotland (TIGLS) must consider an alternative location for the proposal. A spokeswoman for  TGLS said: We are currently reviewing all comments regarding its planning application to Aberdeenshire Council and will issue a comprehensive response which addresses the environmental concerns. cshanks@ajl.co.uk ", "title": "\u00a31 billion golf plan slammed"
"date": "March 26, 2007 Monday", "body": "Homeowners were increasingly turning to fixed-rate mortgages in the face of interest rate uncertainty, research today indicated. It found August's interest rate decision saw four out of 10 homebuyers opt for fixed rates, with the proportion rising to seven out of 10 after November's rise. A third increase announced in January saw this jump to eight out of 10. The study also found both first-time buyers and existing homeowners had turned to fixed rates in increasing numbers. And  around 90% of first time buyers were said to have taken  out fixed rates in January. ", "title": "Buyers opting for fixed rates"

如您所见,每一行都包含一个包含datebodytitle 的列表。我想对每个正文进行分析,例如找到每篇文章中前 20 个单词的词频矩阵(尽管这是以后的事)。目前我只是想打开它。到目前为止,我正在尝试:

import json
data = json.loads(AEE_2007.json)

我知道我犯了一些愚蠢的错误,但相信我,我刚开始用 python 编程......

亲切的问候,

安德烈斯

【问题讨论】:

【参考方案1】:

您拥有的文件不是实际有效的 json,但似乎各行都是。所以我们可以逐行读取并将每个加载为 json 并将结果打包到一个列表中:

with open('AEE_2007.json') as f:
    data = [json.loads(line) for line in f]

需要注意的几点:

我使用json.loads从字符串加载json。 我使用列表理解来构建列表。

list-comprehension 等价于以下内容(但一旦习惯了它就会更快、更容易阅读):

data = []
for line in f:
    data.append(json.loads(line))

【讨论】:

非常感谢,很抱歉再次打扰,但是,我如何阅读每个正文? for obj in data: print obj['body'] 是一种打印方式... 嗨@mgilson,为了一一打开正文...对于数据中的obj: print(obj['body'][[1]]) ? data[0]['body'] 将为您提供data 列表中第一个字典的body【参考方案2】:

您需要打开文件并将文件对象传递给json.load()

import json

with open("AEE_2007.json") as f:
    data = json.load(f)

但是,如果这正是文件的外观 - 它总体上不是一个有效的 json,但这些行是(参见 @mgilson's answer)。

【讨论】:

以上是关于打开JSON文件Python [重复]的主要内容,如果未能解决你的问题,请参考以下文章

getJSON 打开一个 JSON 文件,而不是另一个 [重复]

python,将Json写入文件[重复]

用Python写入json文件[重复]

无法在python中加载json文件[重复]

从python脚本编写Json文件[重复]

在 Python 3.3 中读取 JSON 文件的异常行为 [重复]