微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

如何将.json文件共336个文件与我通过监护人API创建的新闻数据合并?

如何解决如何将.json文件共336个文件与我通过监护人API创建的新闻数据合并?

我通过Guardian API从2019年11月到2020年9月使用新闻数据创建了336个.json文件。每天转换为1个.json文件。由于我想对数据运行情感分析,因此我想首先将所有数据合并到一个单个的.json文件中。在下面,您可以看到我的代码尝试无效。

更新:现在主要要关注的是如何访问所有以日期时间%Y-%m-%d为名称文件。我目前的方法并没有取得太大的成功。使用datetime.Now()仅查找当前日期。但我需要在2019-11-01至2020-09-30之间。

for ind in range(336): 
     file_path_guardian.append(directory_path_news_guardian + datetime.datetime.Now().strftime("%Y-%m-%d") + '.json')

我还附加了一个.json文件中的数据,以便在下面更好地理解。

import json
import numpy as np
import pandas as pd

#Path to the JSON files
directory_path_news_guardian = '/Users/XXX/tempdata/articles_data/'

#Adding all the file names; 
file_path_guardian = []
for ind in range(336): 
 file_path_guardian.append(directory_path_news_guardian + str(ind) + '.json')

#Loading all the JSON files using file names
guardian_news_file=[]
for ind in range(336):
 with open(file_path_guardian[ind]) as f:
   guardian_news_file.append(json.load(f))

#Converting the JSON files to Data Frames
guardian_news = pd.DataFrame()
for ind in range(len(guardian_news_file)):
 guardian_news=guardian_news.append(pd.json_normalize \
                                       (guardian_news_file[ind]))
 #guardian_news['News'] = 'Headline'

错误消息:

JSONDecodeError                           Traceback (most recent call last)
<ipython-input-19-6c1125bb6bef> in <module>
     15 for ind in range(336):
     16  with open(file_path_guardian[ind]) as f:
---> 17    guardian_news_file.append(json.load(f))
     18 
     19 #Converting the JSON files to Data Frames

~/opt/anaconda3/lib/python3.8/json/__init__.py in load(fp,cls,object_hook,parse_float,parse_int,parse_constant,object_pairs_hook,**kw)
    291     kwarg; otherwise ``JSONDecoder`` is used.
    292     """
--> 293     return loads(fp.read(),294         cls=cls,object_hook=object_hook,295         parse_float=parse_float,parse_int=parse_int,~/opt/anaconda3/lib/python3.8/json/__init__.py in loads(s,**kw)
    355             parse_int is None and parse_float is None and
    356             parse_constant is None and object_pairs_hook is None and not kw):
--> 357         return _default_decoder.decode(s)
    358     if cls is None:
    359         cls = JSONDecoder

~/opt/anaconda3/lib/python3.8/json/decoder.py in decode(self,s,_w)
    335 
    336         """
--> 337         obj,end = self.raw_decode(s,idx=_w(s,0).end())
    338         end = _w(s,end).end()
    339         if end != len(s):

~/opt/anaconda3/lib/python3.8/json/decoder.py in raw_decode(self,idx)
    353             obj,end = self.scan_once(s,idx)
    354         except stopiteration as err:
--> 355             raise JSONDecodeError("Expecting value",err.value) from None
    356         return obj,end

JSONDecodeError: Expecting value: line 1 column 1 (char 0)

2019-11-16.json文件示例

  {
    "id": "business/2019/nov/16/are-Voters-ready-for-britains-economy-to-be-rewired","type": "article","sectionId": "business","sectionName": "Business","webPublicationDate": "2019-11-16T16:00:41Z","webTitle": "Are Voters ready for Britain\u2019s economy to be rewired?","webUrl": "https://www.theguardian.com/business/2019/nov/16/are-Voters-ready-for-britains-economy-to-be-rewired","apiUrl": "https://content.guardianapis.com/business/2019/nov/16/are-Voters-ready-for-britains-economy-to-be-rewired","fields": {
      "headline": "Are Voters ready for Britain\u2019s economy to be rewired?","standfirst": "<p>Labour\u2019s plan for free broadband is one of many high-spending promises made by both sides: and they seem to be popular</p>","trailText": "Labour\u2019s plan for free broadband is one of many high-spending promises made by both sides: and they seem to be popular","byline": "Phillip Inman","main": "<figure class=\"element element-image element--showcase\" data-media-id=\"e12db77ec5af9044d6e2e4516e1474ce26b3cbb6\"> <img src=\"https://media.guim.co.uk/e12db77ec5af9044d6e2e4516e1474ce26b3cbb6/0_220_3303_1982/1000.jpg\" alt=\"An Openreach engineer\" width=\"1000\" height=\"600\" class=\"gu-image\" /> <figcaption> <span class=\"element-image__caption\">An Openreach engineer. Labour is proposing to nationalise the network firm as part of its proposals to offer fast broadband free to the whole country. </span> <span class=\"element-image__credit\">Photograph: MONTY_RAKUSEN/BT Openreach</span> </figcaption> </figure>","body": "<p>Voters are becoming more open to calls for <a href=\"https://www.theguardian.com/commentisfree/2019/sep/26/labour-activists-radical-agenda-corbynism\" title=\"\">a radical change</a> to the way the economy is organised and more accepting of the role of government in pushing through significant reforms,according to a major poll of public attitudes last week.</p> <p>Sixty per cent of respondents to a YouGov poll of more than 1,600 people were in favour of the next government making \u201cmoderate\u201d or <a href=\"https://www.theguardian.com/politics/2019/nov/10/labour-activists-call-on-jeremy-corbyn-to-push-radical-stance-on-migration\" title=\"\">\u201cradical\u201d changes to the way the British economy is run</a>,while only 2% said the government should leave the economy as it was. Those in favour of change split between 29% who backed moderate policies and 31% who wanted a more radical agenda.</p> <p>A breakdown of respondents by their political allegiance showed Labour Voters were the biggest supporters of radical change with 59% in favour. But a substantial number of Conservative supporters also said they wanted either \u201cmoderate\u201d (35%) or \u201cradical\u201d (9%) change to the way the economy was run,highlighting demand across the political spectrum for an end to the status quo.</p> <p>The poll,which asked 19 questions about attitudes to government tax and spending policies,goes some way to explain why Boris Johnson\u2019s administration embraced large-scale public investment immediately upon coming to power,and why the Labour party has been grabbing the headlines with radical policies to increase public ownership and investment.</p> <h2>Read the report: <a href=\"https://www.ippr.org/research/publications/paradigm-shift-in-economic-policy\">Public support for a paradigm shift in economic policy</a></h2> <p>The IPPR thinktank,which commissioned the survey,said it was clear from the results that \u201cthe desire for change is widespread across the electorate\u201d and that most people see the government as the main agent of change. Carys Roberts,the IPPR\u2019s chief economist,said it was striking that only 2% of respondents ticked the Box for no change.</p> <p>\u201cAs might be expected,Labour supporters are most in favour of the most dramatic shift,while Conservative supporters were more likely to favour smaller changes,\u201d she said.</p> <p>The poll was conducted before <a href=\"https://www.theguardian.com/technology/2019/nov/15/free-broadband-essential-uk-compete-john-mcdonnell-labour-policy-openreach\" title=\"\">Labour</a><a href=\"https://www.theguardian.com/technology/2019/nov/15/free-broadband-essential-uk-compete-john-mcdonnell-labour-policy-openreach\" title=\"\"> revealed its plan to take control of BT\u2019s broadband business</a>,install a \u00a320bn super-fast public network by 2030 and offer it free to households. Jeremy Corbyn said rolling out full-fibre broadband to every part of the country would,among other benefits,encourage businesses to locate in smaller towns,limiting commuting and carbon emissions.</p> <p>The survey asked who supported or opposed a significant increase in public spending to limit climate change and more than 60% of respondents said they did.</p> <p>Illustrating how green politics has broken out of its leftwing confines to be embraced by a wider audience,a majority of Tory Voters (53%) either strongly agreed or tended to agree,along with 91% of Liberal Democrats and 83% of Labour party supporters.</p> <p>A large minority of Brexit party supporters (48%) backed government intervention to tackle climate change,either strongly or to some extent,but they were opposed by a quarter of those in the party led by Nigel Farage. Around 20% of Tory Voters were opposed to government action compared with 3% of Labour Voters and 2% of Lib Dems.</p>  <figure class=\"element element-image element--showcase\" data-media-id=\"b130ed48d537d98374a0901ac92d9b9376d878ef\"> <img src=\"https://media.guim.co.uk/b130ed48d537d98374a0901ac92d9b9376d878ef/0_297_4800_2880/1000.jpg\" alt=\"Trains at clapham Junction\" width=\"1000\" height=\"600\" class=\"gu-image\" /> <figcaption> <span class=\"element-image__caption\">Respondents to the YouGov survey also supported more spending in a range of areas to combat the climate crisis,including finds for public transport.</span> <span class=\"element-image__credit\">Photograph: Jack Taylor/Getty Images</span> </figcaption> </figure>  <p>YouGov asked whether respondents would support investments in renewable energy,home insulation and public transport as ways for the government to cut carbon,so it cannot be said that the respondents backed investments in digital broadband. However,there appears to be a growing realisation that,11 years after the most devastating financial crash of the postwar era,the direction of travel must change.</p> <p>Most respondents believe the economy is run for the benefit of companies or the richest households,and 44% of Tories \u2013 a surprisingly high number,says Roberts \u2013 support increasing the level of tax on those earning more than \u00a3100,000 a year.</p>  <figure class=\"element element-interactive interactive element--supporting\" data-interactive=\"https://interactive.guim.co.uk/embed/iframe-wrapper/0.1/boot.js\" data-canonical-url=\"https://interactive.guim.co.uk/uploader/embed/2019/11/archive-zip/giv-3902AqGOAN3LCRvr/\" data-alt=\"chart\"> <a href=\"https://interactive.guim.co.uk/uploader/embed/2019/11/archive-zip/giv-3902AqGOAN3LCRvr/\">chart</a> </figure>  <p>Almost 70% of Conservative Voters agreed that capital gains and income from wealth should be taxed more heavily than income from work or at least should be taxed equally. At the moment,the combined effect of income tax and national insurance rates means that workers earning more than \u00a312,000 a year face a higher marginal tax rate than someone drawing hundreds of thousands of pounds a year from investments.</p> <p>Michael Jacobs,a professor of political economy at the University of Sheffield,said the YouGov poll showed the political risk from proposing radical policies was diminishing.</p> <p>\u201cThe public \u2013 including many Conservative Voters \u2013 do not like the way in which inequality has grown and the way in which the economy seems to benefit mainly the wealthy and big business,and they support a wide range of significant reform policies,\u201d he said. \u201cThere is a clear sense here that over the last 10 years since the financial crisis,the centre ground of politics has shifted quite far to the left. At a similar moment after prevIoUs crises \u2013 in the elections of 1945 and 1979 \u2013 our economic model was radically reformed. This election might prove a comparable moment of change.\u201d</p> <p>Keiran Pedley,a research director at pollster Ipsos Mori,was more circumspect,saying that while his firm\u2019s research indicated an increased appetite for extra spending on public services during the past decade \u2013 even if that means some increases to taxes \u2013 <a href=\"https://www.theguardian.com/commentisfree/2018/jan/09/nationalise-rail-gas-water-privately-owned\" title=\"\">Labour was not necessarily the beneficiary</a>.</p> <p>\u201cThe question becomes \u2018what taxes\u2019 and whether Labour is trusted to spend that money. Our recent polling shows the Conservatives are still more trusted on the economy than Labour,so whereas there might be an appetite for change and to increase public spending,Labour still has work to do to prove to Voters that they are up to the job,\u201d he said.</p> <p>The poll also tested the public\u2019s appetite for ministers to play a greater role in \u201cguiding how private companies invest their money\u201d and for a return to collective union bargaining.</p> <p>On both these issues the public was cool,with only 22% offering their support for interfering in private investment decisions and 33% saying they approved of a greater role for Trade unions,compared with 29% saying they disapproved.</p> <p>Labour\u2019s plans to hand shares to workers gained 54% support,much of it from the 43% of Tory Voters who back the idea. There was even more support for a requirement on companies to share their profits with workers more generously (66%) and to have worker representatives in the boardroom,also 66%.</p> <p>David runciman,professor of politics at Cambridge University and the author of <em>The Politics of Good Intentions</em>,said earlier this year that while it might have taken the public more than 10 years to conclude a change of direction was needed,the UK had reached that point.</p> <p>\u201cThe idea of getting back to politics as usual is increasingly absurd. We can\u2019t carry on like this,but we can\u2019t carry on like we used to carry on either,\u201d he said.</p>","newspaperPageNumber": "56","wordcount": "1110","commentable": "false","firstPublicationDate": "2019-11-16T16:00:41Z","isInappropriateForSponsorship": "false","isPremoderated": "false","lastModified": "2020-02-03T12:43:40Z","newspaperEditionDate": "2019-11-17T00:00:00Z","productionOffice": "UK","publication": "The Observer","shortUrl": "https://gu.com/p/cyqaj","shouldHideAdverts": "false","showInRelatedContent": "true","thumbnail": "https://media.guim.co.uk/e12db77ec5af9044d6e2e4516e1474ce26b3cbb6/0_220_3303_1982/500.jpg","legallySensitive": "false","lang": "en","isLive": "true","bodyText": "Voters are becoming more open to calls for a radical change to the way the economy is organised and more accepting of the role of government in pushing through significant reforms,according to a major poll of public attitudes last week. Sixty per cent of respondents to a YouGov poll of more than 1,600 people were in favour of the next government making \u201cmoderate\u201d or \u201cradical\u201d changes to the way the British economy is run,while only 2% said the government should leave the economy as it was. Those in favour of change split between 29% who backed moderate policies and 31% who wanted a more radical agenda. A breakdown of respondents by their political allegiance showed Labour Voters were the biggest supporters of radical change with 59% in favour. But a substantial number of Conservative supporters also said they wanted either \u201cmoderate\u201d (35%) or \u201cradical\u201d (9%) change to the way the economy was run,highlighting demand across the political spectrum for an end to the status quo. The poll,and why the Labour party has been grabbing the headlines with radical policies to increase public ownership and investment. Read the report: Public support for a paradigm shift in economic policy The IPPR thinktank,said it was striking that only 2% of respondents ticked the Box for no change. \u201cAs might be expected,\u201d she said. The poll was conducted before Labour revealed its plan to take control of BT\u2019s broadband business,limiting commuting and carbon emissions. The survey asked who supported or opposed a significant increase in public spending to limit climate change and more than 60% of respondents said they did. Illustrating how green politics has broken out of its leftwing confines to be embraced by a wider audience,along with 91% of Liberal Democrats and 83% of Labour party supporters. A large minority of Brexit party supporters (48%) backed government intervention to tackle climate change,but they were opposed by a quarter of those in the party led by Nigel Farage. Around 20% of Tory Voters were opposed to government action compared with 3% of Labour Voters and 2% of Lib Dems. YouGov asked whether respondents would support investments in renewable energy,the direction of travel must change. Most respondents believe the economy is run for the benefit of companies or the richest households,000 a year. Almost 70% of Conservative Voters agreed that capital gains and income from wealth should be taxed more heavily than income from work or at least should be taxed equally. At the moment,000 a year face a higher marginal tax rate than someone drawing hundreds of thousands of pounds a year from investments. Michael Jacobs,said the YouGov poll showed the political risk from proposing radical policies was diminishing. \u201cThe public \u2013 including many Conservative Voters \u2013 do not like the way in which inequality has grown and the way in which the economy seems to benefit mainly the wealthy and big business,the centre ground of politics has shifted quite far to the left. At a similar moment after prevIoUs crises \u2013 in the elections of 1945 and 1979 \u2013 our economic model was radically reformed. This election might prove a comparable moment of change.\u201d Keiran Pedley,saying that while his firm\u2019s research indicated an increased appetite for extra spending on public services during the past decade \u2013 even if that means some increases to taxes \u2013 Labour was not necessarily the beneficiary. \u201cThe question becomes \u2018what taxes\u2019 and whether Labour is trusted to spend that money. Our recent polling shows the Conservatives are still more trusted on the economy than Labour,\u201d he said. The poll also tested the public\u2019s appetite for ministers to play a greater role in \u201cguiding how private companies invest their money\u201d and for a return to collective union bargaining. On both these issues the public was cool,compared with 29% saying they disapproved. Labour\u2019s plans to hand shares to workers gained 54% support,also 66%. David runciman,professor of politics at Cambridge University and the author of The Politics of Good Intentions,the UK had reached that point. \u201cThe idea of getting back to politics as usual is increasingly absurd. We can\u2019t carry on like this,\u201d he said.","charCount": "6690","shouldHideReaderRevenue": "false","showAffiliateLinks": "false","bylineHtml": "<a href=\"profile/phillipinman\">Phillip Inman</a>"
    },"isHosted": false,"pillarId": "pillar/news","pillarName": "News"
  }
]

我发现了问题。 file_path查找名称为0-335的文件。但是我的文件是按日期命名的。

file_path_guardian.append(directory_path_news_guardian + str(ind) + '.json')

我该如何修改我的代码,而不是str(ind)而是将日期查找为Year-month-day(2019-11-01)?

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。