Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Import data source duplicate field name #12

Open
gnud opened this issue Jul 7, 2022 · 1 comment
Open

Import data source duplicate field name #12

gnud opened this issue Jul 7, 2022 · 1 comment

Comments

@gnud
Copy link

gnud commented Jul 7, 2022

Some of the fields in some of my CSV files start with symbols such as $, %, #; and when the data is cleaned then the symbols are removed automatically and I end up with duplicate fields.

Is there a way to override cleaning the column names before mapping starts?

@sheppard sheppard transferred this issue from wq/django-data-wizard Jul 21, 2022
@sheppard
Copy link
Member

This normalization is handled in IterTable, so I transferred the issue to that repository. The relevant lines are here:

class TupleMapper(DictMapper):
no_pickle_mapper = ['_tuple_class', '_tuple_prototype']
@property
def field_map(self):
field_names = self.get_field_names()
if not field_names and not getattr(self, 'data', None):
raise NoData
# FIXME: check for duplicates
if not hasattr(self, '_field_map'):
items = [
(field, self.tuple_field_name(field))
for field in field_names
]
self._field_map = OrderedDict(items)
return self._field_map
def tuple_field_name(self, field):
field = self.clean_field_name(field)
field = re.sub(r'\W', '', field.lower())
# normalize identifiers for consistency with namedtuple
# http://bugs.python.org/issue23091
field = normalize('NFKC', field)
return field

While this behavior can be overridden, I think it would make sense to fix this in IterTable directly (hence the FIXME). A simple solution for now would be to detect that a normalized name has already been used, and append e.g. _1, _2, _3 etc. to ensure uniqueness.

Eventually we could update Django Data Wizard to support DictMapper in addition to TupleMapper, but that will be a bigger effort.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants