Skip to content

Instantly share code, notes, and snippets.

@mark-kubacki
Created September 20, 2011 16:05
Show Gist options
  • Save mark-kubacki/1229520 to your computer and use it in GitHub Desktop.
Save mark-kubacki/1229520 to your computer and use it in GitHub Desktop.
Beispiel: CSV Dateien einlesen
import csv
__all__ = ['read_csv_file']
def unicode_csv_reader(utf8_data, dialect=csv.excel, **kwargs):
csv_reader = csv.reader(utf8_data, dialect=dialect, **kwargs)
for row in csv_reader:
yield [unicode(cell, 'utf-8', errors='ignore') for cell in row]
def read_csv_file(filename):
with open(filename) as csv_file:
reader = unicode_csv_reader(csv_file)
# first row contains column titles
column_titles = reader.next()
# subsequent rows contain data
for row in reader:
yield dict(zip(column_titles, row))
@mark-kubacki
Copy link
Author

Ferner:

for record in read_csv_file(...):
    send_email(..., template.render(..., **record))

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment