Python创建两个文件的差异

huangapple go评论55阅读模式
英文:

Python create diff of two file

问题

data1 = [open("lis1.json", 'r')]
data2 = [open('list2.json', 'r')]

first_set = set(map(tuple, data1))
second_set = set(map(tuple, data2))

x = (first_set - second_set)
print(x)

我正在尝试查看两个列表,并获取列表1中不在列表2中的数据。问题在于列表1的格式对于两组数据都如下:

data1= ["Jane Doe", 98132]["John Doe", 12345]

data1=
[
    "jane doe",
    98132
][
    "john doe",
    12345
]
   data2=["Jane Doe", 98132]["John drew", 8989]["John drew", 8989]["John drew", 8989]
data2=
[
    "jane doe",
    98132
][
    "john drew",
    8989
]

我期望的结果是

x = [["John Doe", 12345]

我正在比较用户列表与通话日志,因此可能存在重复。

在文件中,我看到错误消息:"Json 标准只允许一个顶级值"。

如果我使用 json(load)

Traceback (most recent call last):
  File "/Users/diff.py", line 4, in <module>
    data1 = json.load(open("list1.json"))
  File "/Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/json/__init__.py", line 293, in load
    return loads(fp.read(),
  File "/Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/json/__init__.py", line 346, in loads
    return _default_decoder.decode(s)
  File "/Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/json/decoder.py", line 340, in decode
    raise JSONDecodeError("Extra data", s, end)
json.decoder.JSONDecodeError: Extra data: line 1 column 19 (char 18)
英文:
data1= [open(&quot;lis1.json&quot;, &#39;r&#39;)]
data2= [open(&#39;list2.json&#39;, &#39;r&#39;)]


first_set = set(map(tuple, data1))
second_set = set(map(tuple, data2))


x= (first_set-second_set)

print(x)

I am attempting to view two list and get the data in list 1 that are not in list two. the issue is list one format for both set of data is as below :

data1= [&quot;Jane Doe&quot;, 98132][&quot;John Doe&quot;, 12345]

or

data1=
[
    &quot;jane doe&quot;,
    98132
][
    &quot;john doe&quot;,
    12345
]
   data2=[&quot;Jane Doe&quot;, 98132][&quot;John drew&quot;, 8989][&quot;John drew&quot;, 8989][&quot;John drew&quot;, 8989]
data2= 
[
    &quot;jane doe&quot;,
    98132
][
    &quot;john drew&quot;,
    8989
]

I was expecting

x = [[&quot;John Doe&quot;, 12345]

I am comparing a list of users vs call logs so it might have duplicate.


in the file I am seeing this in the errors :"Json standard allows only one top-level value"


if I use json(load)

Traceback (most recent call last):
  File &quot;/Users/diff.py&quot;, line 4, in &lt;module&gt;
    data1 = json.load(open(&quot;list1.json&quot;))
  File &quot;/Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/json/__init__.py&quot;, line 293, in load
    return loads(fp.read(),
  File &quot;/Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/json/__init__.py&quot;, line 346, in loads
    return _default_decoder.decode(s)
  File &quot;/Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/json/decoder.py&quot;, line 340, in decode
    raise JSONDecodeError(&quot;Extra data&quot;, s, end)
json.decoder.JSONDecodeError: Extra data: line 1 column 19 (char 18)

答案1

得分: 2

你需要使用 json.load() 来解析 JSON。

data1 = json.load(open("list1.json"))
data2 = json.load(open("list2.json"))
英文:

You need to use json.load() to parse the JSON.

data1 = json.load(open(&quot;list1.json&quot;))
data2 = json.load(open(&quot;list2.json&quot;))

huangapple
  • 本文由 发表于 2023年4月11日 01:36:58
  • 转载请务必保留本文链接:https://go.coder-hub.com/75979345.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定