regex - How to remove commas, brackets in python using regular expression? -



regex - How to remove commas, brackets in python using regular expression? -

these contents of text file (eg:abc.doc):

{'data': [{'name': 'abc'},{'name': 'xyz'}]}

after opening file in python; how remove brackets, quotes , commas. final output should be:

data: name:abc name:xyz

use ast.literal_eval() turn python structure, print values:

with open(r'd:\output1.doc', 'r') inputfile: inputstring = inputfile.read() info = ast.literal_eval(inputstring) key, sublist in data.items(): print '{}:'.format(key) subdict in sublist: key, value in subdict.items(): print('{}:{}'.format(key, value))

for illustration results in:

>>> inputstring = "{'data': [{'name': 'abc'},{'name': 'xyz'}]}" >>> import ast >>> info = ast.literal_eval(inputstring) >>> key, sublist in data.items(): ... print '{}:'.format(key) ... subdict in sublist: ... key, value in subdict.items(): ... print '{}:{}'.format(key, value) ... data: name:abc name:xyz

however: if got facebook api, transcribed format incorrectly. facebook api gives json data, uses double quotes (") instead:

{"data": [{"name": "abc"},{"name": "xyz"}]}

in case should utilize json library comes python:

import json info = json.loads(inputstring) # process same way above.

if have filename, can inquire library read straight file using:

data = json.load(filename) # note, no `s` after `load`.

python regex

Comments

Popular posts from this blog

web services - java.lang.NoClassDefFoundError: Could not initialize class net.sf.cglib.proxy.Enhancer -

Accessing MATLAB's unicode strings from C -

javascript - mongodb won't find my schema method in nested container -