当前位置: 首页 > 知识库问答 >
问题:

正在打开JSON文件Python[重复]

令狐珂
2023-03-14

我正在尝试用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"}

您可以看到,每一行都包含一个带有日期正文标题的列表。我想对每一个主体进行分析,例如,找到每一篇文章中前20个单词的术语频率矩阵(尽管这是稍后的)。现在我只是想打开它。到目前为止,我正在努力:

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

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

共有1个答案

万俟高峻
2023-03-14

您拥有的文件并不是实际有效的json,但看起来单独的行是有效的。因此我们可以逐行读取,并将每一行加载为json,然后将结果打包到一个列表中:

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

需要注意的几件事:

  • 我使用json.loads从字符串加载json
  • 我使用列表理解来构建列表。
data = []
for line in f:
    data.append(json.loads(line))
 类似资料:
  • 问题内容: 说我有这个简单的python脚本: 运行该文件时,第一个打印将打印包含文件文本的列表,而第二个打印将打印空白列表。我猜不是完全出乎意料的。但是,是否有一种方法可以“回退”文件,以便我可以再次读取它?还是最快的方法只是重新打开它? 问题答案: 您可以通过调用重置文件指针: 会做的。您需要在第一行之后写那行。请注意,必须支持随机访问才能使以上功能正常工作。

  • 我在关注以下内容,但仍然没有成功 在sql server 2008中创建了空数据库TestingDb 但它仍然给出了一个错误:系统。数据sqlclient:备份集保存现有“TestDb”数据库以外的数据库的备份

  • 我正在用python编写一个CSV文件,使用以下行: 不幸的是,输出显示了一个无用的列,如下所示: 如何才能摆脱这个无用的第一纵队?

  • 问题内容: 我有一个JSON文件,我想把它弄得一团糟-在python中最简单的方法是什么?我知道PrettyPrint带有一个“对象”,我认为它可以是一个文件,但是我不知道如何传递文件-仅使用文件名不起作用。 问题答案: 该模块已经使用参数实现了一些基本的漂亮打印: 要解析文件,请使用json.load():

  • 我正在导入一个扩展名为csv的文件,并读取行以生成一个列表。 在IDE Spyder中,我在一个选项卡中打开了passwords.csv。当我运行程序时,它给我一个错误,说没有这样的文件或目录。

  • 问题内容: 我有一个脚本读取文件,然后根据该文件完成测试,但是我遇到了一个问题,因为一个小时后文件会重新加载,并且无法在该时间点或之后重新读取该脚本。 所以: 获取要读取的新文件 读取文件 执行文件测试 获取要读取的新文件(具有相同的名称-但如果它是解决方案的一部分,则可以更改) 读取新文件 对新文件执行相同的测试 谁能建议一种使Python重新读取文件的方法? 问题答案: 要么到文件开头 或再次