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

add duplicate column-names with counter #5

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 24 additions & 2 deletions ckanext/xloader/loader.py
Original file line number Diff line number Diff line change
Expand Up @@ -88,8 +88,7 @@ def load_csv(csv_filepath, resource_id, mimetype='text/csv', logger=None):
# headers_dicts = [dict(id=field[0], type=TYPE_MAPPING[str(field[1])])
# for field in zip(headers, types)]

# TODO worry about csv header name problems
# e.g. duplicate names
headers = cleanup_headers(headers)

# encoding (and line ending?)- use chardet
# It is easier to reencode it as UTF8 than convert the name of the encoding
Expand Down Expand Up @@ -242,6 +241,29 @@ def load_csv(csv_filepath, resource_id, mimetype='text/csv', logger=None):
return fields


def cleanup_headers(headers):
unique_headers = []
for header in headers:
if header not in unique_headers:
unique_headers.append(header)
else:
unique_headers = add_header_with_count(unique_headers, header)


return unique_headers


def add_header_with_count(unique_headers, header, count=1):

header_with_count = header + '_' + str(count)
if header_with_count not in unique_headers:
unique_headers.append(header_with_count)
return unique_headers
else:
count += 1
return add_header_with_count(unique_headers, header, count)


def create_column_indexes(fields, resource_id, logger):
logger.info('Creating column indexes (a speed optimization for queries)...')
from ckan import model
Expand Down