fix csv format error
This commit is contained in:
+13
-6
@@ -212,7 +212,7 @@ def _columns_missing(normalized_fieldnames: set[str], required_columns: list[str
|
||||
return [
|
||||
column
|
||||
for column in required_columns
|
||||
if column.strip().upper() not in normalized_fieldnames
|
||||
if _normalize_column_name(column) not in normalized_fieldnames
|
||||
]
|
||||
|
||||
|
||||
@@ -348,7 +348,7 @@ def _parse_single_csv(
|
||||
if not reader.fieldnames:
|
||||
raise CsvValidationError("CSV is missing a header row.")
|
||||
|
||||
normalized_fieldnames = {name.strip().upper() for name in reader.fieldnames if name}
|
||||
normalized_fieldnames = {_normalize_column_name(name) for name in reader.fieldnames if name}
|
||||
csv_format = _detect_csv_format(normalized_fieldnames)
|
||||
|
||||
tests: list[TestRecord] = []
|
||||
@@ -539,6 +539,13 @@ def _normalize_value(value: str | None) -> str:
|
||||
return " ".join(value.strip().upper().split())
|
||||
|
||||
|
||||
def _normalize_column_name(value: str | None) -> str:
|
||||
if value is None:
|
||||
return ""
|
||||
# Accept common header variants, for example: "TC ID", "TC_ID", or "TC\u00A0ID".
|
||||
return re.sub(r"[^A-Z0-9]+", "", value.upper())
|
||||
|
||||
|
||||
def _normalize_yes_no(value: str | None) -> bool:
|
||||
return _normalize_value(value) == "YES"
|
||||
|
||||
@@ -615,15 +622,15 @@ def _row_get(row: dict[str, str], key: str) -> str | None:
|
||||
if key in row:
|
||||
return row.get(key)
|
||||
|
||||
normalized_key = key.strip().upper()
|
||||
normalized_key = _normalize_column_name(key)
|
||||
for existing_key, value in row.items():
|
||||
if existing_key and existing_key.strip().upper() == normalized_key:
|
||||
if existing_key and _normalize_column_name(existing_key) == normalized_key:
|
||||
return value
|
||||
|
||||
if normalized_key == "COE PAIR":
|
||||
if normalized_key == _normalize_column_name("COE PAIR"):
|
||||
for alias in ("COE PAIRING", "COE_PAIRING"):
|
||||
for existing_key, value in row.items():
|
||||
if existing_key and existing_key.strip().upper() == alias:
|
||||
if existing_key and _normalize_column_name(existing_key) == _normalize_column_name(alias):
|
||||
return value
|
||||
|
||||
return None
|
||||
|
||||
Reference in New Issue
Block a user