Skip to content

Commit

Permalink
Feature delimiter option. (#218)
Browse files Browse the repository at this point in the history
  • Loading branch information
danishi authored Jul 10, 2024
1 parent 734dfd4 commit aa11841
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 10 deletions.
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,9 @@ StringListValues=SL
StringSetValues=SS
DecimalListValues=DL
DecimalSetValues=DS

# [DELIMITER_OPTION]
# DelimiterCharacter=|
```

The CSV_SPEC type is mapped to the [DynamoDB attribute type](https://docs.aws.amazon.com/en_us/amazondynamodb/latest/developerguide/HowItWorks.NamingRulesDataTypes.html#HowItWorks.DataTypeDescriptors) in this way.
Expand All @@ -192,6 +195,9 @@ The CSV_SPEC type is mapped to the [DynamoDB attribute type](https://docs.aws.am
Sorry, Binary type and Binary Set type is not supported.
Null type, look [here](https://github.com/danishi/dynamodb-csv?tab=readme-ov-file#import-options).

The default delimiter for list and set types is a space.
If you want to set it, please comment out `DELIMITER_OPTION` and `DelimiterCharacter`.

### Create DynamoDB table

> [!NOTE]
Expand Down
15 changes: 11 additions & 4 deletions app/dynamodb/csv_export.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,12 @@ def csv_export(table: Any, file: str, parameters: Dict = {}) -> Tuple:
except Exception as e:
return (f"CSV specification file can't read:{e}", 1)

# get delimiter options
if "DELIMITER_OPTION" in csv_spec:
delimiter = csv_spec.get("DELIMITER_OPTION", "DelimiterCharacter")
else:
delimiter = " " # default

# write csv
try:
with open(file, mode="w", encoding="utf_8") as f:
Expand Down Expand Up @@ -117,7 +123,7 @@ def csv_export(table: Any, file: str, parameters: Dict = {}) -> Tuple:
del item[key]
continue

item[key] = convert_item(spec, item, key)
item[key] = convert_item(spec, item, key, delimiter)

writer.writerow(item)

Expand All @@ -131,13 +137,14 @@ def csv_export(table: Any, file: str, parameters: Dict = {}) -> Tuple:
return (str(e), 1)


def convert_item(spec: str, item: Dict, key: str) -> Any:
def convert_item(spec: str, item: Dict, key: str, delimiter: str) -> Any:
"""convert item
Args:
spec (str): type of item
row (Dict): row data
key (str): key
delimiter (str): list join delimiter
Returns:
Any: converted item value
Expand All @@ -154,9 +161,9 @@ def convert_item(spec: str, item: Dict, key: str) -> Any:
elif spec == "J": # Json
return json.dumps(item[key], default=decimal_encode)
elif spec == "SL" or spec == "SS": # StringList or StringSet
return " ".join(item[key])
return delimiter.join(item[key])
elif spec == "DL" or spec == "DS": # DecimalList or DecimalSetDecimalList
return " ".join(list(map(str, item[key])))
return delimiter.join(list(map(str, item[key])))
else:
return item[key]

Expand Down
19 changes: 13 additions & 6 deletions app/dynamodb/csv_import.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,12 @@ def csv_import(table: Any, file: str, ignore: bool = False) -> Tuple:
except Exception as e:
return (f"CSV specification file can't read:{e}", 1)

# get delimiter options
if "DELIMITER_OPTION" in csv_spec:
delimiter = csv_spec.get("DELIMITER_OPTION", "DelimiterCharacter")
else:
delimiter = " "

# read csv
try:
with open(file, mode="r", encoding="utf_8") as f:
Expand Down Expand Up @@ -64,7 +70,7 @@ def csv_import(table: Any, file: str, ignore: bool = False) -> Tuple:
continue

try:
row[key] = convert_column(spec, row, key)
row[key] = convert_column(spec, row, key, delimiter)
except Exception:
del row[key]

Expand All @@ -85,13 +91,14 @@ def csv_import(table: Any, file: str, ignore: bool = False) -> Tuple:
return (f"CSV file can't read:{e}", 1)


def convert_column(spec: str, row: Dict, key: str) -> Any:
def convert_column(spec: str, row: Dict, key: str, delimiter: str) -> Any:
"""convert column
Args:
spec (str): type of column
row (Dict): row data
key (str): key
delimiter (str): list split delimiter
Returns:
Any: converted column value
Expand All @@ -107,13 +114,13 @@ def convert_column(spec: str, row: Dict, key: str) -> Any:
elif spec == "J": # Json
return json.loads(row[key], parse_float=Decimal)
elif spec == "SL": # StringList
return row[key].split()
return row[key].split(delimiter)
elif spec == "SS": # StringSet
return set(row[key].split())
return set(row[key].split(delimiter))
elif spec == "DL": # DecimalList
return list(map(Decimal, row[key].split()))
return list(map(Decimal, row[key].split(delimiter)))
elif spec == "DS": # DecimalSet
return set(list(map(Decimal, row[key].split())))
return set(list(map(Decimal, row[key].split(delimiter))))
else:
return row[key]

Expand Down
3 changes: 3 additions & 0 deletions sample.csv.spec
Original file line number Diff line number Diff line change
Expand Up @@ -25,3 +25,6 @@ DecimalSetValues=DS
# [IMPORT_OPTION]
# ConvertBlankToNullAttrs=NullValue,JsonValue
# ConvertBlankToDropAttrs=DecimalValue

# [DELIMITER_OPTION]
# DelimiterCharacter=|
3 changes: 3 additions & 0 deletions sample_exp.csv.spec
Original file line number Diff line number Diff line change
Expand Up @@ -21,3 +21,6 @@ StringListValues=SL
StringSetValues=SS
DecimalListValues=DL
DecimalSetValues=DS

[DELIMITER_OPTION]
DelimiterCharacter=|

0 comments on commit aa11841

Please sign in to comment.