Python创建两个文件的差异

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

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]

  1. data1=
  2. [
  3. "jane doe",
  4. 98132
  5. ][
  6. "john doe",
  7. 12345
  8. ]
  9. data2=["Jane Doe", 98132]["John drew", 8989]["John drew", 8989]["John drew", 8989]
  10. data2=
  11. [
  12. "jane doe",
  13. 98132
  14. ][
  15. "john drew",
  16. 8989
  17. ]

我期望的结果是

x = [["John Doe", 12345]

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

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

如果我使用 json(load)

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

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

or

  1. data1=
  2. [
  3. &quot;jane doe&quot;,
  4. 98132
  5. ][
  6. &quot;john doe&quot;,
  7. 12345
  8. ]
  9. data2=[&quot;Jane Doe&quot;, 98132][&quot;John drew&quot;, 8989][&quot;John drew&quot;, 8989][&quot;John drew&quot;, 8989]
  10. data2=
  11. [
  12. &quot;jane doe&quot;,
  13. 98132
  14. ][
  15. &quot;john drew&quot;,
  16. 8989
  17. ]

I was expecting

  1. 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)

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

答案1

得分: 2

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

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

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

  1. data1 = json.load(open(&quot;list1.json&quot;))
  2. 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:

确定