Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/324.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 从文件夹中读取json文件,获取特定的键值,将键值写入文件夹中以分隔文本文件_Python_Json - Fatal编程技术网

Python 从文件夹中读取json文件,获取特定的键值,将键值写入文件夹中以分隔文本文件

Python 从文件夹中读取json文件,获取特定的键值,将键值写入文件夹中以分隔文本文件,python,json,Python,Json,我需要在多个json文件中提取键“text”的值,并将该值写入UTF-8格式的单独文本文件 有2900个json文件,我已经将json、os、glob导入到运行python 3.7 64位、W10、VS studio的环境中,并将当前工作目录设置为json文件所在的文件夹 python:这将成功地将一个json文件“j_news1”中的键“text”值写入一个文本文件“j_news1.txt: with open('j_news1.txt', 'w') as json_file: json.du

我需要在多个json文件中提取键“text”的值,并将该值写入UTF-8格式的单独文本文件

有2900个json文件,我已经将json、os、glob导入到运行python 3.7 64位、W10、VS studio的环境中,并将当前工作目录设置为json文件所在的文件夹

python:这将成功地将一个json文件“j_news1”中的键“text”值写入一个文本文件“j_news1.txt:

with open('j_news1.txt', 'w') as json_file:
json.dump(j_news1['text'], json_file)
--我已为cwd中的所有文件创建了文件列表:

filelist = glob.glob(',/*.json')
获取文件列表并为文件夹中的所有json打开读取、打开写入的代码:

for fname in filelist:
       FI = open(fname, 'r')
       FO = open(fname.replace('json', 'txt'), 'w')
       for line in FI:
          FO.write(line)
我找不到的是如何只提取键“text”并将其写入单独的文本文件

文件中的json示例:

pprint.pprint(j_news1)
{'author': '',
 'crawled': '2018-02-02T15:37:30.069+02:00',
 'entities': {'locations': [{'name': 'us', 'sentiment': 'none'}],
              'organizations': [{'name': 'mins ago cnbc',
                                 'sentiment': 'negative'}],
              'persons': [{'name': 'kate rogers', 'sentiment': 'negative'}]},
 'external_links': [],
 'highlightText': '',
 'highlightTitle': '',
 'language': 'english',
 'locations': [],
 'ord_in_thread': 0,
 'organizations': [],
 'persons': [],
 'published': '2018-02-02T14:39:00.000+02:00',
 'text': "Police officer shortage growing problem in US 54 Mins Ago CNBC's "
         'Kate Rogers reports police departments around the country are facing '
         'a major challenge recruiting and retaining staff.',
 'thread': {'country': 'US',
            'domain_rank': 767,
            'main_image': 'https://fm.cnbc.com/applications/cnbc.com/resources/img/editorial/2018/02/02/104984906-6ED2-SB-020218-PoliceShortage.600x400.jpg',
            'participants_count': 0,
            'performance_score': 0,
            'published': '2018-02-02T14:39:00.000+02:00',
            'replies_count': 0,
            'section_title': 'Latest Video',
            'site': 'cnbc.com',
            'site_full': 'www.cnbc.com',
            'site_section': 'http://www.cnbc.com/id/100004038/device/rss/rss.html',
            'site_type': 'news',
            'social': {'facebook': {'comments': 0, 'likes': 1, 'shares': 1},
                       'gplus': {'shares': 0},
                       'linkedin': {'shares': 0},
                       'pinterest': {'shares': 0},
                       'stumbledupon': {'shares': 0},
                       'vk': {'shares': 0}},
            'spam_score': 0.0,
            'title': 'Police officer shortage growing problem in US',
            'title_full': '',
            'url': 'https://www.cnbc.com/video/2018/02/02/police-officer-shortage-growing-problem-in-us.html',
            'uuid': 'b55dd31fa1152ca4b7fff55bc143129add534a41'},
 'title': 'Police officer shortage growing problem in US',
 'url': 'https://www.cnbc.com/video/2018/02/02/police-officer-shortage-growing-problem-in-us.html',
 'uuid': 'b55dd31fa1152ca4b7fff55bc143129add534a41'}
非常简单:将json文件加载到python对象,然后从中提取
'text'

import json
json_object = json.load(FI)
FO.write(json_object['text'])

就这样。

谢谢makozaki,我在OpenStatement中添加了encoding='UTF-8',现在我有了一个解决方案,非常感谢我的帮助,如果它解决了您的问题,那么您可以接受我的答案;)