Add support for xlsx files
This commit is contained in:
@@ -1,7 +1,8 @@
|
||||
import csv
|
||||
import re
|
||||
from typing import List, Dict, Union, TextIO
|
||||
from io import TextIOBase
|
||||
from io import TextIOBase, StringIO
|
||||
from xlsx2csv import Xlsx2csv
|
||||
from dateutil import parser
|
||||
from pathlib import Path
|
||||
from rich.console import Console
|
||||
@@ -24,7 +25,7 @@ def list_key_values(data: List[Dict], key):
|
||||
output.discard(None)
|
||||
return output
|
||||
|
||||
def read_and_normalize_csv(input_file: Union[List[TextIO], List[Path], TextIO, Path]) -> List[dict]:
|
||||
def read_and_normalize_csv_or_xlsx(input_file: Union[List[TextIO], List[Path], TextIO, Path]) -> List[dict]:
|
||||
"""
|
||||
Reads CSV file(s) from the provided input file path(s) or file object(s),
|
||||
and returns a list of dictionaries with normalized keys and values
|
||||
@@ -51,8 +52,18 @@ def read_and_normalize_csv(input_file: Union[List[TextIO], List[Path], TextIO, P
|
||||
|
||||
for f in file_list:
|
||||
if isinstance(f, Path):
|
||||
f = f.open()
|
||||
reader = csv.DictReader(f)
|
||||
if f.suffix.lower() == ".csv":
|
||||
with f.open("r", encoding="utf-8") as f:
|
||||
reader = csv.DictReader(f)
|
||||
|
||||
elif f.suffix.lower() == ".xlsx":
|
||||
output = StringIO()
|
||||
Xlsx2csv(f, outputencoding="utf-8").convert(output)
|
||||
output.seek(0)
|
||||
reader = csv.DictReader(output)
|
||||
|
||||
else:
|
||||
raise ValueError("File must be a .csv or .xlsx")
|
||||
for row in reader:
|
||||
normalized_row = normalize_row(row, normalization_config)
|
||||
result_data.append(normalized_row)
|
||||
@@ -157,7 +168,7 @@ def parse_datetime(data: List[Dict]):
|
||||
|
||||
def import_gamebygame(data: Union[List[Dict], TextIO, Path]) -> List[Dict]:
|
||||
if isinstance(data, TextIOBase) or isinstance(data, Path) :
|
||||
data = read_and_normalize_csv(data)
|
||||
data = read_and_normalize_csv_or_xlsx(data)
|
||||
|
||||
header = data[0].keys()
|
||||
visitor_home_order_reversed = is_visitor_home_order_reversed(list(header))
|
||||
|
||||
Reference in New Issue
Block a user