-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexport_google_serp_csv.py
More file actions
33 lines (23 loc) · 1.01 KB
/
Copy pathexport_google_serp_csv.py
File metadata and controls
33 lines (23 loc) · 1.01 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
"""Export Google SERP Dataset JSON to CSV."""
from __future__ import annotations
import argparse
import csv
import json
from pathlib import Path
from typing import Any
FIELDS = ["query", "page", "position", "title", "url", "description"]
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-google-results.csv"))
args = parser.parse_args()
export_csv(args.input, args.output)
print(f"Wrote {args.output}")
if __name__ == "__main__":
main()