forked from respec/HSPsquared
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathreadCSV.py
45 lines (37 loc) · 1.29 KB
/
readCSV.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
''' Copyright (c) 2020 by RESPEC, INC.
Author: Robert Heaphy, Ph.D.
License: LGPL2
'''
from pandas import read_csv, read_excel, HDFStore
def readCSV(csvname, hdfname, datapath):
'''
Updates or creates DataFrame in HDF5 file from CSV file.
Does not append new rows to existing HDF5 table
Parameters
----------
csvname : str
name of CSV,TSV, XLSX file to update/create HDF5
hdfname : str
Name of HDF5 file to be updated
datapath : str
Pathname in HDF5 file to be update/created
Returns
-------
None.
'''
with HDFStore(hdfname) as store:
if datapath in store:
df = store[datapath]
df.update(read(csvname, indexcol=0))
df.to_hdf(store, datapath, data_columns=True, format='t')
else:
df = read(csvname, indexcol=0)
df.to_hdf(store, datapath, data_columns=True, format='t')
return
def read(csvname, indexcol=None):
if csvname.lower().endswith('.csv'): return read_csv(csvname, index_col=0)
elif csvname.lower().endswith('.tsv'): return read_csv(csvname, index_col=0, delimiter='\t')
elif csvname.lower().endswith('xlsx'): return read_excel(csvname, index_col=0)
else:
print('CSV file has unknown file extension')
return None