How To Do Json Format
(S (PERSON Rami/NNP Eid/NNP) is/VBZ studying/VBG at/IN (ORGANIZATION Stony/NNP Brook/NNP University/NNP) in/IN (LOCATION NY/NNP)) This is output of NLTK code now i
Solution 1:
This should do it.
import json
with open('data.txt', 'w') as outfile:
json.dump(data, outfile)
Solution 2:
nltk = """
(S
(PERSON Rami/NNP Eid/NNP)
is/VBZ
studying/VBG
at/IN
(ORGANIZATION Stony/NNP Brook/NNP University/NNP)
in/IN
(LOCATION NY/NNP)) """from pyparsing import Suppress, ungroup, Word, alphas, Group, Dict, OneOrMore
LPAR,RPAR,SLASH = map(Suppress,"()/")
parsed_word = ungroup(Word(alphas) + Suppress(SLASH + Word(alphas)))
named_token = Group(LPAR + Word(alphas)("name") +
OneOrMore(parsed_word).setParseAction(' '.join)("value") +
RPAR)
subject = (Suppress("S") + named_token)
nltk_expr = (LPAR + subject("subject") +
Dict(OneOrMore(named_token | Suppress(parsed_word)))("predicate") +
RPAR)
defmake_subject_main_key(t):
subname = t.pop('subject')[0].value
subdesc = t.pop('predicate')
t[subname] = subdesc
nltk_expr.setParseAction(make_subject_main_key)
print nltk_expr.parseString(nltk).asDict()
prints
{'Rami Eid': {'ORGANIZATION': 'Stony Brook University', 'LOCATION': 'NY'}}
Solution 3:
This will give you nicely formatted json file.
withopen('/path/to/your/new_json_file.json', "w") as f:
f.write(json.dumps(data, indent=4, sort_keys=True))
This will create a file your_json_file.json
in in your/
directory with.
{"GuruRaj Bagali":{"job":"professor","location":"NY"},"Rami Eid":{"ORGANIZATION":"Stony Brook University","location":"NY"}}
The indentation and sort_keys arguments are optional. Depending on how you're using it they may be nice to have.
You could also put the same data on one line and wouldn't need to import json.
with open('/path/to/your/new_json_file.json', "w") as f:
f.write('%s' % data)
Will create
{'Rami Eid': {'ORGANIZATION': 'Stony Brook University', 'location': 'NY'}, 'GuruRaj Bagali': {'job': 'professor', 'location': 'NY'}}
Post a Comment for "How To Do Json Format"