-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstandardize_csv.py
executable file
·72 lines (54 loc) · 1.99 KB
/
standardize_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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
#!/usr/bin/env python
import io
from sys import stdout, stdin, stderr
import clevercsv
import chardet
"""
Script permettant de lire un csv sur l'entrée standard, et de retourner sur la sortie un CSV RFC-4180 en UTF-8.
Le script detecte l'encoding (chardet) et le dialect (clevercsv).
Exemples :
Utilisation basique :
cat moncsvpourri.csv | python standardize_csv.py > moncsvclean.csv
Pour afficher seulement les caractéristiques du csv (encoding et dialect) :
cat moncsvpourri.csv | python standardize_csv.py > /dev/null
Utilisation avec ogr2ogr :
cat moncsvpourri.csv | python standardize_csv.py | ogr2ogr target.gpkg CSV:/vsistdin/
TODO : I/O fichiers
TODO2 : Générer un csvt ?
"""
def standardize():
def rename_duplicates(lst):
"""
Pour renommer les entêtes identiques (col, 1_col, 2_col, etc.)
"""
seen = {}
new_lst = []
for item in lst:
if item not in seen:
seen[item] = 1
new_lst.append(item)
else:
seen[item] += 1
new_lst.append(f"{seen[item] - 1}_{item}")
return new_lst
input_data = stdin.buffer.read() # read as binary
encoding_detection = chardet.detect(input_data)
encoding, confidence = encoding_detection['encoding'], round(encoding_detection['confidence'],2)
print('Encoding : {} (with {} confidence)'.format(encoding, confidence), file=stderr)
csvfile = io.StringIO(input_data.decode(encoding))
dialect = clevercsv.Sniffer().sniff(csvfile.read())
print('Dialect : {}'.format(dialect.to_dict()), file=stderr)
csvfile.seek(0)
reader = clevercsv.reader(csvfile, dialect)
rows = reader
## Write new file
writer = clevercsv.write.writer(stdout, encoding='utf8') #Print to stdout
header = True
for row in rows :
if header :
row = rename_duplicates(row)
pass
header = False
writer.writerow(row)
if __name__ == '__main__':
standardize()