Skip to content

Instantly share code, notes, and snippets.

@jacobmischka
Last active January 19, 2024 21:27
Show Gist options
  • Save jacobmischka/6398cd82b634ae402a211787559ec5d7 to your computer and use it in GitHub Desktop.
Save jacobmischka/6398cd82b634ae402a211787559ec5d7 to your computer and use it in GitHub Desktop.
import csv, os, sys
"""
A bash script I also looked at for a while, but ended up being less useful than just a filtered view of the CSVs.
function diff-clients() {
xsv select '!"Icon","Last seen","Channel","Signal"' "$1" | xsv sort -s MAC -o /tmp/before.csv
xsv select '!"Icon","Last seen","Channel","Signal"' "$2" | xsv sort -s MAC -o /tmp/after.csv
csv-diff --key MAC /tmp/before.csv /tmp/after.csv
xsv table /tmp/before.csv -o /tmp/before.txt
xsv table /tmp/after.csv -o /tmp/after.txt
delta -s /tmp/before.txt /tmp/after.txt
}
"""
"""
Usage: python3 csvdiff.py <path to before csv> <path to after csv> [--added]
Output is sent to stdout. By default, output entries present in "before" but missing from "after".
Pass `--added` to reverse this and output entries present in "after" that weren't in "before".
"""
def main():
before_path = sys.argv[1]
after_path = sys.argv[2]
show_added = "--added" in sys.argv
with open(before_path, "r") as before_file, open(after_path, "r") as after_file:
before_rows = list(csv.DictReader(before_file))
after_rows = list(csv.DictReader(after_file))
writer = csv.DictWriter(sys.stdout, before_rows[0].keys())
writer.writeheader()
if show_added:
before_macs = {row["MAC"] for row in before_rows}
added = [row for row in after_rows if row["MAC"] not in before_macs]
writer.writerows(added)
else:
after_macs = {row["MAC"] for row in after_rows}
missing = [row for row in before_rows if row["MAC"] not in after_macs]
writer.writerows(missing)
if __name__ == "__main__":
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment