Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #46301

Re: how to compare two json file line by line using python?

Path csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!feeder.erje.net!us.feeder.erje.net!news.linkpendium.com!news.linkpendium.com!panix!not-for-mail
From Grant Edwards <invalid@invalid.invalid>
Newsgroups comp.lang.python
Subject Re: how to compare two json file line by line using python?
Date Tue, 28 May 2013 16:00:14 +0000 (UTC)
Organization PANIX Public Access Internet and UNIX, NYC
Lines 34
Message-ID <ko2kae$9oh$3@reader1.panix.com> (permalink)
References <355f934e-bda0-4316-96bb-583c498ecb1a@googlegroups.com> <51a2f016$0$1744$c3e8da3$76491128@news.astraweb.com>
NNTP-Posting-Host dsl.comtrol.com
X-Trace reader1.panix.com 1369756814 10001 64.122.56.22 (28 May 2013 16:00:14 GMT)
X-Complaints-To abuse@panix.com
NNTP-Posting-Date Tue, 28 May 2013 16:00:14 +0000 (UTC)
User-Agent slrn/1.0.1 (Linux)
Xref csiph.com comp.lang.python:46301

Show key headers only | View raw


On 2013-05-27, Steven D'Aprano <steve+comp.lang.python@pearwood.info> wrote:
> On Sun, 26 May 2013 21:32:40 -0700, Avnesh Shakya wrote:
>
>> But I want to compare line by line and value by value. but i found that
>> json data is unordered data, so how can i compare them without sorting
>> it. please give me some idea about it. I am new for it. I want to check
>> every value line by line.
>
> Why do you care about checking every value line by line? As you say 
> yourself, JSON data is unordered, so "line by line" is the wrong way to 
> compare it.

There's no such thing as "lines" in JSON anyway. Outside of string
literals, all whitespace is equivalent, so replacing all newlines with
space characters results in equivalent blobs of JSON -- but one is a
single line, and the other is multiple lines.

> The right way is to decode the JSON data, and then compare whether it 
> gives you the result you expect:
>
> a = json.load("file-a")
> b = json.load("file-b")
> if a == b:
>     print("file-a and file-b contain the same JSON data")
>
> If what you care about is the *data* stored in the JSON file, this is
> the correct way to check it.

Yup.

-- 
Grant Edwards               grant.b.edwards        Yow! Are we laid back yet?
                                  at               
                              gmail.com            

Back to comp.lang.python | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

how to compare two json file line by line using python? Avnesh Shakya <avnesh.nitk@gmail.com> - 2013-05-26 21:32 -0700
  Re: how to compare two json file line by line using python? rusi <rustompmody@gmail.com> - 2013-05-26 21:51 -0700
    Re: how to compare two json file line by line using python? Avnesh Shakya <avnesh.nitk@gmail.com> - 2013-05-27 10:35 +0530
  Re: how to compare two json file line by line using python? Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2013-05-27 05:33 +0000
    Re: how to compare two json file line by line using python? Avnesh Shakya <avnesh.nitk@gmail.com> - 2013-05-27 11:58 +0530
    Re: how to compare two json file line by line using python? Grant Edwards <invalid@invalid.invalid> - 2013-05-28 16:00 +0000
  Re: how to compare two json file line by line using python? Denis McMahon <denismfmcmahon@gmail.com> - 2013-05-27 10:50 +0000

csiph-web