Skip to content

Instantly share code, notes, and snippets.

@vitan
Created January 20, 2015 08:01
Show Gist options
  • Save vitan/61d1ba42192a9ab3539d to your computer and use it in GitHub Desktop.
Save vitan/61d1ba42192a9ab3539d to your computer and use it in GitHub Desktop.
How to load .csv as dict in Python, skipping initial space and quotechar
#!/usr/bin/env python
"""
Given file example.csv, with the following contents:
key1: 'I am value1'
key2: 'I am value2'
"""
import csv
example = dict()
with open('example.csv') as csvfile:
reader = csv.reader(csvfile, delimiter=':', skipintialspace=True, quotechar="'")
example = {row[0]: row[1] for row in reader}
"""
example = {'key1': 'I am value1', 'key2': 'I am value2'}
"""
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment