Last active
July 4, 2023 11:38
-
-
Save Himura2la/70c2165e801d2672e45ec8a1e97d9066 to your computer and use it in GitHub Desktop.
CSV Supplementer
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import json | |
import os | |
import csv | |
from urllib.request import urlopen, Request, HTTPError | |
csv_path = os.path.expanduser(r"~\Desktop\data.csv") | |
additional_cols = [] | |
def fetch_additional_data(row): | |
# TODO | |
return [] | |
def fetch(url, method=None, data=None, headers={}): | |
try: | |
if data: | |
data = data.encode('ascii') | |
req = Request(url, method=method, data=data, headers=headers) | |
with urlopen(req) as resp: | |
return json.loads(resp.read().decode('utf-8')) | |
except HTTPError as e: | |
print("Request failed:", e) | |
return None | |
with open(csv_path, 'r', encoding='utf-8') as f: | |
reader = csv.reader(f) | |
head = reader.__next__() | |
data = [row for row in reader] | |
csv_path_root, csv_path_ext = os.path.splitext(csv_path) | |
output_path = f'{csv_path_root}_processed{csv_path_ext}' | |
with open(output_path, 'w', newline='', encoding='utf-8') as f: | |
writer = csv.writer(f) | |
writer.writerow(head + additional_cols) | |
for row in data: | |
with open(output_path, 'a', newline='', encoding='utf-8') as f: | |
writer = csv.writer(f) | |
writer.writerow(row + fetch_additional_data(row)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment