-
Notifications
You must be signed in to change notification settings - Fork 0
/
file_reader.py
104 lines (86 loc) · 2.76 KB
/
file_reader.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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
# _*_ coding: utf-8 _*_
import os
import json
import numpy as np
def save(data, file_name, path, type):
if path[-1] != '/':
destination = path + "/" + file_name
else:
destination = path + file_name
f = open(destination, type)
f.write(data)
f.close()
print("Saved.")
def load(file_name, path):
if file_name != '':
source = path + "/" + file_name
else:
source = path
if os.path.exists(source):
f = open(source, 'rb')
data = []
for i in f:
elem = []
try:
elem = i.decode('utf-8')
except:
try:
elem = i.decode('big5')
except:
try:
elem = i.decode('x-windows-950')
except:
elem = i.decode('ISO-8859-1')
elem = elem.encode(encoding='utf-8')
if len(elem) > 0:
data.append(elem.replace('"', '').replace('\r\n', '').split(','))
f.close()
return data
else:
print("This file doesn't exist.")
# def load_xls(file_name, path):
# if file_name != '':
# source = path + "/" + file_name
# else:
# source = path
#
# if os.path.exists(source):
# sheets = json.dumps(get_data(source))
# data = []
# for line in (sheets.replace('{', '').replace('}', '').replace('Sheet1', '').replace(':', '').replace('"', '')
# .replace('[', '').replace(']]', ']').split('],')):
# for element in line:
# # if element.find('\\u') != -1:
# # element = element
# # print(element, end='')
# data.append(element.split(','))
#
# return data
# else:
# print("This xls file doesn't exist.")
def load_all(data, path):
if os.path.exists(path):
if os.path.isdir(path):
filelist = os.listdir(path)
for f in filelist:
load_all(data, os.path.join(path, f))
elif os.path.isfile(path):
buffer = []
# if path.find('.xls') != -1:
# index = path.rfind('/')
# print(path[index+1:])
# buffer = load_xls('', path)
if path.find('HOURLY') != -1:
index = path.rfind('/')
print(path[index+1:])
buffer = load('', path)
if path.find('.csv') != -1:
index = path.rfind('/')
print(path[index + 1:])
buffer = load('', path)
if len(buffer) != 0:
data.append(buffer)
else:
print("Loading error.")
else:
print("This path doesn't exist.")