Path: csiph.com!usenet.pasdenom.info!news.redatomik.org!newsfeed.xs4all.nl!newsfeed3a.news.xs4all.nl!xs4all!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.000 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'else:': 0.03; 'output': 0.05; 'subject:Python': 0.06; 'column': 0.07; 'json': 0.07; 'lines,': 0.07; 'skip:` 10': 0.07; 'subject:Error': 0.07; 'string': 0.09; 'ascii': 0.09; 'data:': 0.09; 'fname': 0.09; 'subject:files': 0.09; 'python': 0.11; "%s'": 0.16; '(char': 0.16; '307': 0.16; 'dirname,': 0.16; 'dump': 0.16; 'filelist': 0.16; 'folder.': 0.16; 'from:addr:mrabarnett.plus.com': 0.16; 'from:addr:python': 0.16; 'from:name:mrab': 0.16; 'message- id:@mrabarnett.plus.com': 0.16; 'none.': 0.16; 'program?': 0.16; 'received:192.168.1.4': 0.16; 'received:84.93': 0.16; 'received:84.93.230': 0.16; 'followed': 0.16; 'wrote:': 0.18; 'file,': 0.19; 'written': 0.21; 'command': 0.22; 'header:User- Agent:1': 0.23; 'error': 0.23; 'text,': 0.24; 'header:In-Reply- To:1': 0.27; "skip:' 10": 0.31; 'extract': 0.31; 'such.': 0.31; 'file': 0.32; 'skip:m 30': 0.32; 'run': 0.32; 'another': 0.32; 'info': 0.35; 'received:84': 0.35; 'version': 0.36; 'data,': 0.36; 'skip:j 20': 0.36; 'should': 0.36; 'wrong': 0.37; 'writes': 0.38; 'to:addr:python-list': 0.38; 'files': 0.38; 'to:addr:python.org': 0.39; 'read': 0.60; 'new': 0.61; 'back': 0.62; 'more': 0.64; 'reads': 0.68; 'skip:r 30': 0.69; "'new'": 0.84; 'directory:': 0.84; 'opens': 0.91 X-CM-Score: 0.00 X-CNFS-Analysis: v=2.1 cv=EJWI0jpC c=1 sm=1 tr=0 a=0nF1XD0wxitMEM03M9B4ZQ==:117 a=0nF1XD0wxitMEM03M9B4ZQ==:17 a=0Bzu9jTXAAAA:8 a=mlFM_a_ONtUA:10 a=IkcTkHD0fZMA:10 a=EBOSESyhAAAA:8 a=n9JCsJWBQDXN0glniUUA:9 a=QEXdDO2ut3YA:10 X-AUTH: mrabarnett@:2500 Date: Tue, 31 Mar 2015 00:18:06 +0100 From: MRAB User-Agent: Mozilla/5.0 (Windows NT 6.3; WOW64; rv:31.0) Gecko/20100101 Thunderbird/31.5.0 MIME-Version: 1.0 To: python-list@python.org Subject: Re: Error in processing JSON files in Python References: <63a251f6-01ff-4662-ab9c-588e3ffcd73a@googlegroups.com> In-Reply-To: <63a251f6-01ff-4662-ab9c-588e3ffcd73a@googlegroups.com> Content-Type: text/plain; charset=utf-8; format=flowed Content-Transfer-Encoding: 7bit X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.19 Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: Lines: 54 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1427757495 news.xs4all.nl 2881 [2001:888:2000:d::a6]:49228 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:88352 On 2015-03-30 22:27, Karthik Sharma wrote: > 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('
')[0] > version = ms.split('version:')[1].strip().split('
')[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` > open(fname, 'r+') opens the file for update, json.load(f) reads from the file, and then json.dump(json_data,f) writes back to the file, _appending_ to it, so the file now contains the old data followed by the new data. Another point: "null" is a string and will be written as such. If you actually want a null in the JSON data, then that should be None.