-
Notifications
You must be signed in to change notification settings - Fork 51
Expand file tree
/
Copy pathread_csv.py
More file actions
executable file
·27 lines (23 loc) · 1001 Bytes
/
Copy pathread_csv.py
File metadata and controls
executable file
·27 lines (23 loc) · 1001 Bytes
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
#!/usr/bin/env python
import csv
from argparse import ArgumentParser
def main():
arg_parser = ArgumentParser(description='reads csv file, summarize')
arg_parser.add_argument('--file', type=str, action='store',
dest='file', help='CSV file to parse')
options = arg_parser.parse_args()
with open(options.file, 'r') as csv_file:
dialect = csv.Sniffer().sniff(csv_file.read(1024))
csv_file.seek(0)
sum = 0.0
csv_reader = csv.DictReader(csv_file, fieldnames=None,
restkey='rest', restval=None,
dialect=dialect)
print('headers: {0}'.format(':'.join(csv_reader.fieldnames)))
for row in csv_reader:
print('{name} --- {weight}'.format(name=row['name'],
weight=row['weight']))
sum += float(row['weight'])
print(f'sum = {sum}')
if __name__ == '__main__':
main()