-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcsv_rw_skiprows.py
More file actions
89 lines (75 loc) · 2.55 KB
/
csv_rw_skiprows.py
File metadata and controls
89 lines (75 loc) · 2.55 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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
# Copyright (c) 2023, NVIDIA CORPORATION & AFFILIATES. All rights reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
"""
simple helper functions for reading and
saving CSV files
"""
import csv
import numpy as np
def csv_to_dict(filename, mapping=None, delimiter=",", skiprows=0):
"""
reads a csv file to a dictionary of columns
Parameters
----------
filename : str
The file name to load from
mapping : None, dict
If None load entire csv file and store
every column as a key in the dict. If
`mapping` is not none use this to map
keys from CSV to keys in dict.
delimiter: str
The string used for separating values.
Returns
-------
data : dict of numpy arrays
numpy arrays have shape [N, 1].
"""
# Load csv file
values = np.loadtxt(filename, skiprows=skiprows+1, delimiter=delimiter, unpack=False)
# get column keys
csvfile = open(filename)
reader = csv.reader(csvfile, delimiter=delimiter)
first_line = next(reader)
for i in range(skiprows-1):
first_line = next(reader)
# set dictionary
csv_dict = {}
for i, name in enumerate(first_line):
if mapping is not None:
if name.strip() in mapping.keys():
csv_dict[mapping[name.strip()]] = values[:, i : i + 1]
else:
csv_dict[name.strip()] = values[:, i : i + 1]
return csv_dict
def dict_to_csv(dictonary, filename):
"""
saves a dict of numpy arrays to csv file
Parameters
----------
dictionary : dict
dictionary of numpy arrays. The numpy
arrays have a shape of [N, 1].
filename : str
The file name to save too
"""
# add csv to filename
if filename[-4:] != ".csv":
filename += ".csv"
# save np arrays
csvfile = open(filename, "w+")
csvfile.write(",".join(['"' + str(x) + '"' for x in list(dictonary.keys())]) + "\n")
for i in range(next(iter(dictonary.values())).shape[0]):
csvfile.write(",".join([str(x[i, 0]) for x in dictonary.values()]) + "\n")
csvfile.close()