{"slug": "diffing-ai-answer-snapshots-over-time-with-python", "title": "Diffing AI Answer Snapshots Over Time With Python", "summary": "A Hong Kong-based overseas marketing team at PandaWM published a Python approach for tracking how AI assistants describe a brand over time, saving weekly plain-text answer snapshots and diffing them with difflib. The script normalizes whitespace and splits answers into sentences before diffing, flags whether a brand mention was gained or lost, and prints only changed sentences. The team cautions that answers vary between runs, so single-week changes should be treated as hints and paired with referrer and CRM tracking to confirm AI-referred traffic.", "body_md": "If you track how AI assistants describe your brand, you quickly collect a folder of saved answers: one text file per question per week. Reading them all is slow. A small diff script shows what changed, which is usually the only thing worth reviewing.\n\n```\nsnapshots/\n  2026-10-17/q01-best-agency-hk.txt\n  2026-10-24/q01-best-agency-hk.txt\n  2026-10-31/q01-best-agency-hk.txt\n```\n\nEach file holds the plain-text answer you saved for that question that week, collected manually or through whatever tool your team is allowed to use.\n\n``` python\nimport difflib, pathlib, re, sys\n\nBRAND = re.compile(r\"pandawm|熊猫出海\", re.I)\nroot = pathlib.Path(\"snapshots\")\nweeks = sorted(p.name for p in root.iterdir() if p.is_dir())\nprev, curr = weeks[-2], weeks[-1]\n\ndef norm(text):\n    text = re.sub(r\"\\s+\", \" \", text)\n    return [s.strip() for s in re.split(r\"(?<=[.!?。！？])\\s*\", text) if s.strip()]\n\nfor f in sorted((root / curr).glob(\"*.txt\")):\n    old_f = root / prev / f.name\n    old = norm(old_f.read_text(encoding=\"utf-8\")) if old_f.exists() else []\n    new = norm(f.read_text(encoding=\"utf-8\"))\n    was, now = any(BRAND.search(s) for s in old), any(BRAND.search(s) for s in new)\n    changed = [l for l in difflib.unified_diff(old, new, lineterm=\"\", n=0)\n               if l[:1] in \"+-\" and not l.startswith((\"+++\", \"---\"))]\n    flag = \"GAINED\" if now and not was else \"LOST\" if was and not now else \"\"\n    print(f\"\\n## {f.stem}  {flag}  ({len(changed)} changed sentences)\")\n    for l in changed[:12]:\n        print(l)\n```\n\nSplitting into sentences before diffing matters. Assistants rephrase constantly, and a character-level diff would mark everything as changed.\n\nAnswers vary from run to run even on the same day. Treat one week's change as a hint, and look for patterns across several weeks before acting. Save the date, the tool and the exact prompt with every snapshot so comparisons stay fair.\n\nVisibility only matters if it turns into visits and leads. Pair this with referrer tracking and CRM fields so you can see whether a gained mention coincides with more AI-referred traffic; this [attributing AI search leads with UTM and CRM](https://www.pandawm.com/blog/attribute-google-ai-leads-with-utm-crm.html) walks through that setup.\n\n*Published by [PandaWM](https://www.pandawm.com/), an overseas marketing team based in Hong Kong.*", "url": "https://wpnews.pro/news/diffing-ai-answer-snapshots-over-time-with-python", "canonical_source": "https://dev.to/pandawm/diffing-ai-answer-snapshots-over-time-with-python-f38", "published_at": "2026-10-30 21:01:07+00:00", "updated_at": "2026-10-30 21:20:46.802543+00:00", "lang": "en", "topics": ["generative-engine-optimization", "ai-search"], "entities": ["PandaWM"], "also_reported_by": [], "alternates": {"html": "https://wpnews.pro/news/diffing-ai-answer-snapshots-over-time-with-python", "markdown": "https://wpnews.pro/news/diffing-ai-answer-snapshots-over-time-with-python.md", "text": "https://wpnews.pro/news/diffing-ai-answer-snapshots-over-time-with-python.txt", "jsonld": "https://wpnews.pro/news/diffing-ai-answer-snapshots-over-time-with-python.jsonld"}}