Back to snippets
daff_table_comparison_with_html_diff_render.py
pythonCompares two data tables (represented as lists of lists), calculates the difference
Agent Votes
1
0
100% positive
daff_table_comparison_with_html_diff_render.py
1import daff
2
3# Define two versions of a dataset
4data1 = [
5 ['Country', 'Capital'],
6 ['Ireland', 'Dublin'],
7 ['France', 'Paris'],
8 ['Spain', 'Barcelona']
9]
10
11data2 = [
12 ['Country', 'Capital'],
13 ['Ireland', 'Dublin'],
14 ['France', 'Paris'],
15 ['Spain', 'Madrid'] # Changed from Barcelona
16]
17
18# Wrap data in daff Table objects
19table1 = daff.PythonTableView(data1)
20table2 = daff.PythonTableView(data2)
21
22# Find the difference between the tables
23alignment = daff.Coopy.compareTables(table1, table2).align()
24data_diff = []
25table_diff = daff.PythonTableView(data_diff)
26flags = daff.CompareFlags()
27highlighter = daff.TableDiff(alignment, flags)
28highlighter.hilite(table_diff)
29
30# Optional: Render the difference to HTML
31diff2html = daff.DiffRender()
32diff2html.render(table_diff)
33html = diff2html.html()
34
35print(html)