Skip to content

Instantly share code, notes, and snippets.

@GISmd
Last active June 23, 2016 20:59
Show Gist options
  • Save GISmd/09cd2a8bef73ebb0d89f76d17c8da2c7 to your computer and use it in GitHub Desktop.
Save GISmd/09cd2a8bef73ebb0d89f76d17c8da2c7 to your computer and use it in GitHub Desktop.
Functions for writing and reading dictionaries of lists to and from a CSV file.
def write_dict_to_csv(dictionary, filename):
"""Write a dictionary of lists to a CSV file with the keys as the column
heading. Will work with lists of unequal length."""
with open(filename, 'w') as myfile:
myfile.write(','.join(dictionary.keys())) # write header
for line in itertools.izip_longest(*dictionary.values(), fillvalue=''):
myfile.write('\n' + ','.join(line))
print "Created file: " + os.getcwd() + '\\' + filename
def read_dict_from_csv(filename):
"""Read CSV file and read file into a dictionary of lists. First row becomes
the keys to in the dictionary and each column of data is input as a list
assigned to their respective key"""
with open(filename, 'r') as myfile:
lines = myfile.read().split('\n')
keys = lines[0].split(',')
# dictionary = {}
dictionary = collections.OrderedDict()
for key in keys:
dictionary[key] = []
for line in lines[1:]:
if not line == '': # skip empty lines
for i, item in enumerate(line.split(',')):
if item:
dictionary.items()[i][1].append(item)
return dictionary
def increment_filename(dictionary, filename):
"""Looks in current working directory, finds all filenames that match
filename + digits + .csv, determines the highest number, increments by
1 and returns the new filename. Intended to be used with
write_dict_to_csv()"""
#get list of manifests, determine the highest number, add
one and create new manifest file
cwd = os.getcwd()
expression = filename + r'\d+' + r'\.csv'
file_matches = [item for item in os.listdir(cwd) if re.match(expression, item)]
# mani_list = glob.glob(cwd + r'\manifest[0-9].csv')
if file_matches:
nums = []
for f in file_matches:
# Get digits in current file and join them
nums.append(int(''.join([str(s) for s in os.path.split(f)[1] if s.isdigit()])))
output_filename = filename + str((max(nums) + 1)) + '.csv'
else:
output_filename = filename + r'1.csv'
return output_filename
def write_manifest(manifest):
"""Take dictionary called manifest from get_manifest() and write it to
a file. This is an example usage of increment_filename() and
write_dict_to_csv()"""
filename = increment_filename(manifest, 'manifest')
write_dict_to_csv(manifest, filename)
return filename
# Example usage
manifest = {'Name':['Bob', 'Sarah', 'Henry'], 'EyeColor':['Brown', 'Blue', 'Brown']}
write_manifest(manifest)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment