-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathloaddata.py
More file actions
64 lines (54 loc) · 1.99 KB
/
loaddata.py
File metadata and controls
64 lines (54 loc) · 1.99 KB
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
import os
import csv
def get_files_from_path(path: str, extension: str=None) -> list:
"""return list of files from path"""
# see the answer on the link below for a ridiculously
# complete answer for this. I tend to use this one.
# note that it also goes into subdirs of the path
# https://stackoverflow.com/a/41447012/9267296
result = []
for subdir, dirs, files in os.walk(path):
for filename in files:
filepath = subdir + os.sep + filename
if extension == None:
result.append(filepath)
elif filename.lower().endswith('.csv'):
result.append(filepath)
return result
def load_csv(filename: str) -> list:
"""load a CSV file and return it as a list of dict items"""
result = []
with open(filename) as infile:
reader = csv.reader(infile)
# get the column names
# https://stackoverflow.com/a/28837325/9267296
# doing this as you state that you're dealing with
# x/y and x/z values
column0, column1 = next(reader)
for line in reader:
try:
result.append({column0: float(line[0]),
column1: float(line[1])})
except Exception as e:
# I always print out error messages
# in case of random weird things
print(e)
continue
return result
def load_all(path: str) -> dict:
"""loads all CSV files into a dict"""
result = {}
csvfiles = get_files_from_path(path)
for filename in csvfiles:
# extract the filename without extension
# and us it as key name
# since we only load .csv files we can just
# remove the last 4 characters from filename
keyname = os.path.basename(filename)[:-4]
result[keyname] = load_csv(filename)
return result
from pprint import pprint
all = load_all('path/to/csv/files')
pprint(all)
print('\n--------------------\n')
pprint(all['11 z 205'])