-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtoolkit_text.py
181 lines (139 loc) · 5.13 KB
/
toolkit_text.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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
import re
import base64
import csv
import pandas as pd
#######################################################################
def decode_base64(data):
"""Decode base64, padding being optional.
:param data: Base64 data as an ASCII byte string
:returns: The decoded byte string.
"""
missing_padding = len(data) % 4
if missing_padding != 0:
data += '=' * (4 - missing_padding)
return base64.urlsafe_b64decode(data).decode('utf-8')
def encode_base64(data):
"""
base64 encode
"""
return base64.urlsafe_b64encode(data.encode()).decode('utf-8')
########################################################################
# Regex
def regex_find(pattern, text):
'''
return list
'''
match_return = re.compile(pattern, re.IGNORECASE).findall(text)
return match_return
def regex_replace(pattern, string, text):
'''
Replace pattern with string in text
'''
return re.sub(pattern, string, text, flags=re.IGNORECASE)
def regex_replace_file(FILENAME, pattern, string, exception=None):
'''
Replace the regex pattern with string of a file, except exceptions
'''
import logging
with open(FILENAME, 'r') as f:
text = f.read()
if exception and re.compile(exception, re.IGNORECASE).findall(text):
logging.debug(" - Skip " + exception)
return
with open(FILENAME, 'w') as f:
f.write(re.sub(pattern, string, text, flags=re.IGNORECASE))
########################################################################
########################################################################
# csv <-> list
def csv2list(FILE):
'''Import csv to list'''
csv_list = []
with open(FILE, encoding='utf-8') as csvfile:
# Detect header, remove if exists
has_header = csv.Sniffer().has_header(csvfile.read(1024))
csvfile.seek(0) # Rewind.
reader = csv.reader(csvfile)
if has_header:
# print("Header detected, skip.")
next(reader) # Skip header row
return list(csv.reader(csvfile, delimiter=','))
def list2csv(list_, FILE):
listHeader = ['col1', 'col2']
with open(FILE, 'w', encoding='utf-8', newline='') as f:
writer = csv.writer(f)
f.write(', '.join(listHeader))
f.write('\n')
writer.writerows(list_)
########################################################################
########################################################################
# csv <-> dict
def dict2csv(dictList, fileName):
'''
Input: dictList
Output: csv file
'''
keys = dictList[0].keys()
with open(fileName, 'w', encoding='utf-8', newline='') as output_file:
dict_writer = csv.DictWriter(output_file, keys)
dict_writer.writeheader()
dict_writer.writerows(dictList)
def csv2dict(csv_file):
'''
Iutput: csv file
Onput: dictList
'''
dataFrame = pd.read_csv(csv_file, encoding='utf-8', header=0)
# print(data.to_dict())
dict_list = []
for i in dataFrame.index:
data_dict = {}
for column in dataFrame.columns:
data_dict[column] = dataFrame[column][i]
dict_list.append(data_dict)
return dict_list
########################################################################
def fwf2dict(FILE, widthList):
'''Transform fixed width file into dict'''
# Specify header = 0 to set the first line as header
# Specify "names = [col1, col2, ...]" to set the header from [col1, col2, ...]
dataFrame = pd.read_fwf(FILE, header=None, widths=widthList, )
return dataFrame.to_dict()
########################################################################
def convert_to_Byte(value):
'''Convert KB, MB, GB into Byte'''
UNIT_SET = ['B', 'KB', 'MB', 'GB', 'TB', 'PB']
for i, c in enumerate(value):
if c not in '0123456789-.':
break
number = float(value[:i])
unit = value[i:]
inByte = number * 1024 ** UNIT_SET.index(unit)
return int(inByte)
def convert_Bytes(size):
'''Convert Byte into KB, MB, GB'''
power = 2**10
n = 0
Dic_powerN = {0: 'B', 1: 'KB', 2: 'MB', 3: 'GB', 4: 'TB'}
while size > power:
size /= power
n += 1
return '{}{}'.format(round(size, 2), Dic_powerN[n])
def convert_timezone(date_str, locale='Asia/Shanghai'):
'''Convert 2019-01-10T12:09:52-0500 to local time'''
import dateutil.parser
import pytz
date = dateutil.parser.parse(date_str)
return date.astimezone(pytz.timezone(locale)).strftime('%Y-%m-%d %H:%M:%S %Z')
def chunks(lst, n):
"""Yield successive n-sized chunks from list.
Usage:
list(chunks(toolkit_file.get_file_list(SOURCE_FOLDER), 6))
"""
for i in range(0, len(lst), n):
yield lst[i:i + n]
if __name__ == '__main__':
x = fwf2dict('test.txt', [8, 16, 16, 12, 14, 16, 7, ])
print(x)
# _ = csv2dict('FL_insurance_sample.csv')
# print(_)
# 'Account', 'LastName', 'FirstName', 'Balance', 'CreditLimit', 'AccountCreated', 'Rating',