Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #88349
| Newsgroups | comp.lang.python |
|---|---|
| Date | 2015-03-30 14:27 -0700 |
| Message-ID | <63a251f6-01ff-4662-ab9c-588e3ffcd73a@googlegroups.com> (permalink) |
| Subject | Error in processing JSON files in Python |
| From | Karthik Sharma <karthik.sharma@gmail.com> |
I have the following python program to read a set of JSON files do some processing on it and dump them back to the same folder. However When I run the below program and then try to see the output of the JSON file using
`cat file.json | python -m json.tool`
I get the following error
`extra data: line 1 column 307 - line 1 column 852 (char 306 - 851)`
What is wrong with my program?
#Process 'new' events to extract more info from 'Messages'
rootDir = '/home/s_parts'
for dirName, subdirList, fileList in os.walk(rootDir):
print('Found directory: %s' % dirName)
for fname in fileList:
fname='s_parts/'+fname
with open(fname, 'r+') as f:
json_data = json.load(f)
et = json_data['Et']
ms = json_data['Ms']
if (event == 'a.b.c.d') or (event == 'e.f.g.h'):
url = re.sub('.+roxy=([^& ]*).*', r'\1', ms)
nt = re.findall(r"NT:\s*([^,)]*)",ms)[0]
bt = re.findall(r"BT:\s*([^,)]*)",ms)[0]
xt = re.findall(r"XT:\s*([^,)]*)",ms)[0]
appde = ms.split('Appde:')[1].strip().split('<br>')[0]
version = ms.split('version:')[1].strip().split('<br>')[0]
json_data["url"] = url
json_data["BT"] = bt
json_data["XT"] = xt
json_data["NT"] = nt
json_data["Appde"] = appde
json_data["version"] = version
else:
json_data["url"] = "null"
json_data["BT"] = "null"
json_data["XT"] = "null"
json_data["NT"] = "null"
json_data["Appde"] = "null"
json_data["version"] = "null"
json.dump(json_data,f)
If I do a `file` command on the output file I get
`s_parts/data_95: ASCII text, with very long lines, with no line terminators`
Back to comp.lang.python | Previous | Next — Next in thread | Find similar | Unroll thread
Error in processing JSON files in Python Karthik Sharma <karthik.sharma@gmail.com> - 2015-03-30 14:27 -0700 Re: Error in processing JSON files in Python MRAB <python@mrabarnett.plus.com> - 2015-03-31 00:18 +0100 Re: Error in processing JSON files in Python Denis McMahon <denismfmcmahon@gmail.com> - 2015-03-31 19:58 +0000
csiph-web