-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.py
33 lines (29 loc) · 854 Bytes
/
utils.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
import base64
import io
import pandas as pd
def parse_contents(contents, filename):
"""decode csv file
Args:
contents ([type]): [description]
filename (str): path
Returns:
[type]: [description]
"""
content_type, content_string = contents.split(',')
decoded = base64.b64decode(content_string)
try:
if 'csv' in filename:
# Assume that the user uploaded a CSV file
df = pd.read_csv(
io.StringIO(decoded.decode('latin1')),
sep=';' ,
encoding = "ISO-8859-1"
)
return df
elif 'xls' in filename:
# Assume that the user uploaded an excel file
df = pd.read_excel(io.BytesIO(decoded))
return df
except Exception as e:
print(e)
return None