-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCsv.py
33 lines (30 loc) · 972 Bytes
/
Csv.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
#!/usr/bin/python
# -*- coding: iso-8859-15 -*-
# Extending objects with CSV interfaces.
# The method should be simply deduce (such as csvClass)
# This module provides some function for a lot of funnythings to
# such as parseCsvClass which load a full file.
#
# System import
import csv
import sys
# User import
def load(filename, key, value):
"""On charge un élément depuis un fichier particulier en cherchant une valeur dans une clef
On retourne un dictionnaire de résultat ou une liste vide (None)."""
reader = csv.DictReader(open(filename, 'rb'))
try:
for row in reader:
if row[key] == value:
return row
except csv.Error, e:
sys.exit("file %s, line %d: %s" % (filename, csvReader.line_num, e))
# If not found, return none
return None
def loadall(filename):
"""On charge toutes les lignes d'un CSV dans une liste de tuples"""
retour = []
reader = csv.DictReader(open(filename, 'rb'))
for row in reader:
retour.append(row)
return retour