Skip to content

Instantly share code, notes, and snippets.

@pzwang
Created December 17, 2014 15:29
Show Gist options
  • Save pzwang/91f0cd7e8bb9ee84553a to your computer and use it in GitHub Desktop.
Save pzwang/91f0cd7e8bb9ee84553a to your computer and use it in GitHub Desktop.
improved fieldparse
# fieldparse.py
import collections
def parse(lines,types,names):
if not isinstance(lines, collections.Iterable) or isinstance(lines, str):
raise TypeError("lines must be an iterable container of strings")
if not isinstance(types, collections.Sequence) or \
not isinstance(names, collections.Sequence):
raise TypeError("types and names must be sequences")
if len(types) != len(names):
raise ValueError("types and names must have the same number of items")
if not all(isinstance(x, collections.Callable) for x in types):
raise TypeError("types must be callables")
records = []
for line in lines:
# Strip whitespace and check for blank lines
line = line.strip()
if not line: continue
# Split into fields
fields = line.split()
if len(fields) != len(names):
raise RuntimeError("Wrong number of columns in input")
# Apply conversions to each field
cfields = [converter(value) for converter,value in zip(types,fields)]
# Create a dictionary from the data
record = dict(zip(names,cfields))
records.append(record)
return records
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment