Answer the question
In order to leave comments, you need to log in
How to make a loop of reading files from a folder, processing and writing them to a json file in python?
There is a program that reads text from a file, processes it and writes it to a json file. How to make it read all the files in the folder, process and write to separate json files?
import json
from natasha import (
NamesExtractor,
PersonExtractor,
LocationExtractor,
AddressExtractor,
OrganisationExtractor,
DatesExtractor,
)
from natasha.markup import show_markup, show_json
extractors = [ NamesExtractor(),
PersonExtractor(),
LocationExtractor(),
AddressExtractor(),
OrganisationExtractor(),
DatesExtractor(),
]
with open('test.txt', 'r') as f:
text = f.read()
spans = []
facts = []
for extractor in extractors:
matches = extractor(text)
spans.extend(_.span for _ in matches)
facts.extend(_.fact.as_json for _ in matches)
show_json(facts)
f = open("output.json",'w', encoding='utf-8')
f.write(json.dumps(facts, ensure_ascii=False))
f.close()
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question