how to do Json format [duplicate]

2019-09-22 05:40发布

This question is an exact duplicate of:

 (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 want to store it in json file like

import json

data = {
        'Rami Eid':{'ORGANIZATION': 'Stony Brook University',  'location':'NY'},
        'GuruRaj Bagali':{'job': 'professor', 'location': 'NY'}
       }

I want store chunk tree into json file like above format how to do?

3条回答
狗以群分
2楼-- · 2019-09-22 06:01

This should do it.

import json
with open('data.txt', 'w') as outfile:
    json.dump(data, outfile)
查看更多
狗以群分
3楼-- · 2019-09-22 06:05

This will give you nicely formatted json file.

 with open('/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'}}

查看更多
Emotional °昔
4楼-- · 2019-09-22 06:14
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)

def make_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'}}
查看更多
登录 后发表回答