This repository has been archived by the owner on Apr 19, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
read.py
78 lines (58 loc) · 1.62 KB
/
read.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
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
import csv
from io import StringIO, BytesIO
import docx
import openpyxl
"""
Reading functions
"""
def read_docx(doc_byte):
"""
Read doc bytes (required for form data)
"""
doc = docx.Document(BytesIO(doc_byte))
return doc
def read_csv(csv_byte):
"""
Read csv bytes (required for form data)
"""
csv_string = StringIO(csv_byte.decode())
csv_reader = csv.DictReader(csv_string)
return csv_reader
def read_xlsx(xlsx_byte):
"""
Read xlsx bytes (required for form data)
"""
xlsx_io = BytesIO(xlsx_byte)
xlsx = openpyxl.load_workbook(filename=xlsx_io)
xlsx_sheet = xlsx.active
xlsx_data = xlsx_sheet.rows
csv_string = ''
for row in xlsx_data:
l = list(row)
for i in range(len(l)):
if i == len(l) - 1:
if l[i].value == None:
csv_string +=''
else:
csv_string+= '"' + str(l[i].value) + '"'
else:
if l[i].value == None:
csv_string +=','
else:
csv_string += '"' + str(l[i].value) + '",'
csv_string+='\n'
csv_string = StringIO(csv_string)
csv_reader = csv.DictReader(csv_string, quotechar='"')
return csv_reader
def read_csv_reader(csv_reader):
line = 0
headers = csv_reader.fieldnames
data = []
for row in csv_reader:
data.append(row)
line+=1
print(' Processed %d lines' % line)
csv_data = dict()
csv_data['headers'] = headers
csv_data['data'] = data
return csv_data