1
0
mirror of https://github.com/ilri/csv-metadata-quality.git synced 2025-05-10 07:06:00 +02:00

Add "unsafe fixes" runtime option

In this case it fixes occurences of invalid multi-value separators.
DSpace uses "||" to separate multiple values in one field, but our
editors sometimes give us files with mistakes like "|". We can fix
these to be correct multi-value separators if we are sure that the
metadata is not actually using "|" for some legitimate purpose.
This commit is contained in:
2019-07-28 22:53:39 +03:00
parent a93b5b31c5
commit 40e77db713
4 changed files with 46 additions and 1 deletions

View File

@ -35,3 +35,33 @@ def whitespace(field):
new_field = '||'.join(values)
return new_field
def separators(field):
"""Fix for invalid multi-value separators (ie "|")."""
# Skip fields with missing values
if pd.isna(field):
return
# Initialize an empty list to hold the cleaned values
values = list()
# Try to split multi-value field on "||" separator
for value in field.split('||'):
# After splitting, see if there are any remaining "|" characters
pattern = re.compile(r'\|')
match = re.findall(pattern, value)
if len(match) > 0:
print(f'Fixing invalid multi-value separator: {value}')
value = re.sub(pattern, '||', value)
# Save cleaned value
values.append(value)
# Create a new field consisting of all values joined with "||"
new_field = '||'.join(values)
return new_field