-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexport_bing_serp_csv.py
More file actions
46 lines (36 loc) · 1.14 KB
/
Copy pathexport_bing_serp_csv.py
File metadata and controls
46 lines (36 loc) · 1.14 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
"""Flatten a Bing Dataset JSON export into a CSV file."""
from __future__ import annotations
import argparse
import csv
import json
from pathlib import Path
from typing import Any
FIELDS = [
"type",
"query",
"country",
"market",
"page",
"position",
"title",
"url",
"sourceDomain",
"description",
"date",
"paginationStoppedReason",
]
def export_csv(input_path: Path, output_path: Path) -> None:
rows: list[dict[str, Any]] = json.loads(input_path.read_text(encoding="utf-8"))
with output_path.open("w", encoding="utf-8", newline="") as handle:
writer = csv.DictWriter(handle, fieldnames=FIELDS, extrasaction="ignore")
writer.writeheader()
writer.writerows(rows)
def main() -> None:
parser = argparse.ArgumentParser(description=__doc__)
parser.add_argument("input", type=Path, nargs="?", default=Path("data/sample-output.json"))
parser.add_argument("output", type=Path, nargs="?", default=Path("data/exported-bing-results.csv"))
args = parser.parse_args()
export_csv(args.input, args.output)
print(f"Wrote {args.output}")
if __name__ == "__main__":
main()