Skip to content

Commit

Permalink
Merge pull request #17 from arborworkflows/single_column_table
Browse files Browse the repository at this point in the history
Fix romanesco's loading of single column tables
  • Loading branch information
zackgalbreath committed Sep 19, 2014
2 parents 05e2879 + 275af78 commit eb62b07
Showing 1 changed file with 18 additions and 10 deletions.
28 changes: 18 additions & 10 deletions romanesco/format/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -92,16 +92,24 @@ def csv_to_rows(input):
# csv package does not support unicode
input = str(input)

# Take a data sample to determine headers, but
# don't include incomplete last line
sample = ''
sampleSize = 0
while len(sample) == 0:
sampleSize += 5000
sample = '\n'.join(input[:sampleSize].splitlines()[:-1])
dialect = csv.Sniffer().sniff(sample)
dialect.skipinitialspace = True
header = has_header(sample, dialect)
# Special case: detect single-column files.
# This check assumes that our only valid delimiters are commas and tabs.
firstLine = input.split('\n')[0]
if not ('\t' in firstLine or ',' in firstLine):
header = False
dialect = 'excel'

else:
# Take a data sample to determine headers, but
# don't include incomplete last line
sample = ''
sampleSize = 0
while len(sample) == 0:
sampleSize += 5000
sample = '\n'.join(input[:sampleSize].splitlines()[:-1])
dialect = csv.Sniffer().sniff(sample)
dialect.skipinitialspace = True
header = has_header(sample, dialect)

if header:
reader = csv.DictReader(input.splitlines(), dialect=dialect)
Expand Down

0 comments on commit eb62b07

Please sign in to comment.