英文:
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("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)
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= ["Jane Doe", 98132]["John Doe", 12345]
or
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
]
I was expecting
x = [["John Doe", 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 "/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)
答案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("list1.json"))
data2 = json.load(open("list2.json"))
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论