-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathfile_handling.py
243 lines (208 loc) · 9.91 KB
/
file_handling.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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
import pandas as pd
from tkinter.filedialog import askopenfilename, asksaveasfilename, askopenfilenames
import os
from tkinter import Tk
def open_unknown_csv(file_in, delimination):
# Hide Tkinter GUI
Tk().withdraw()
encode_index = 0
encoders = ['utf_8', 'latin1', 'utf_16',
'ascii', 'big5', 'big5hkscs', 'cp037', 'cp424',
'cp437', 'cp500', 'cp720', 'cp737', 'cp775',
'cp850', 'cp852', 'cp855', 'cp856', 'cp857',
'cp858', 'cp860', 'cp861', 'cp862', 'cp863',
'cp864', 'cp865', 'cp866', 'cp869', 'cp874',
'cp875', 'cp932', 'cp949', 'cp950', 'cp1006',
'cp1026', 'cp1140', 'cp1250', 'cp1251', 'cp1252',
'cp1253', 'cp1254', 'cp1255', 'cp1256', 'cp1257',
'cp1258', 'euc_jp', 'euc_jis_2004', 'euc_jisx0213', 'euc_kr',
'gb2312', 'gbk', 'gb18030', 'hz', 'iso2022_jp',
'iso2022_jp_1', 'iso2022_jp_2', 'iso2022_jp_2004', 'iso2022_jp_3', 'iso2022_jp_ext',
'iso2022_kr', 'latin_1', 'iso8859_2', 'iso8859_3', 'iso8859_4',
'iso8859_5', 'iso8859_6', 'iso8859_7', 'iso8859_8', 'iso8859_9',
'iso8859_10', 'iso8859_11', 'iso8859_13', 'iso8859_14', 'iso8859_15',
'iso8859_16', 'johab', 'koi8_r', 'koi8_u', 'mac_cyrillic',
'mac_greek', 'mac_iceland', 'mac_latin2', 'mac_roman', 'mac_turkish',
'ptcp154', 'shift_jis', 'shift_jis_2004', 'shift_jisx0213', 'utf_32',
'utf_32_be', 'utf_32_le', 'utf_16', 'utf_16_be', 'utf_16_le',
'utf_7', 'utf_8', 'utf_8_sig']
data = open_file(file_in, encoders[encode_index], delimination)
encoder = encoders[encode_index]
while type(data) == str:
print("Encoder error: " + encoders[encode_index])
if encode_index < len(encoders) - 1:
encode_index = encode_index + 1
try:
print("Trying encoder: " + encoders[encode_index])
data = open_file(file_in, encoders[encode_index], delimination)
except:
continue
print("")
encoder = encoders[encode_index]
else:
print("Can't find appropriate encoder")
input("Program Terminated. Press Enter to continue...")
exit()
return data
def encoder_finder(file_in, delimination):
encode_index = 0
encoders = ['utf_8', 'latin1', 'utf_16',
'ascii', 'big5', 'big5hkscs', 'cp037', 'cp424',
'cp437', 'cp500', 'cp720', 'cp737', 'cp775',
'cp850', 'cp852', 'cp855', 'cp856', 'cp857',
'cp858', 'cp860', 'cp861', 'cp862', 'cp863',
'cp864', 'cp865', 'cp866', 'cp869', 'cp874',
'cp875', 'cp932', 'cp949', 'cp950', 'cp1006',
'cp1026', 'cp1140', 'cp1250', 'cp1251', 'cp1252',
'cp1253', 'cp1254', 'cp1255', 'cp1256', 'cp1257',
'cp1258', 'euc_jp', 'euc_jis_2004', 'euc_jisx0213', 'euc_kr',
'gb2312', 'gbk', 'gb18030', 'hz', 'iso2022_jp',
'iso2022_jp_1', 'iso2022_jp_2', 'iso2022_jp_2004', 'iso2022_jp_3', 'iso2022_jp_ext',
'iso2022_kr', 'latin_1', 'iso8859_2', 'iso8859_3', 'iso8859_4',
'iso8859_5', 'iso8859_6', 'iso8859_7', 'iso8859_8', 'iso8859_9',
'iso8859_10', 'iso8859_11', 'iso8859_13', 'iso8859_14', 'iso8859_15',
'iso8859_16', 'johab', 'koi8_r', 'koi8_u', 'mac_cyrillic',
'mac_greek', 'mac_iceland', 'mac_latin2', 'mac_roman', 'mac_turkish',
'ptcp154', 'shift_jis', 'shift_jis_2004', 'shift_jisx0213', 'utf_32',
'utf_32_be', 'utf_32_le', 'utf_16', 'utf_16_be', 'utf_16_le',
'utf_7', 'utf_8', 'utf_8_sig']
data = open_file(file_in, encoders[encode_index], delimination)
encoder = encoders[encode_index]
while type(data) == str:
print("Encoder error: " + encoders[encode_index])
if encode_index < len(encoders) - 1:
encode_index = encode_index + 1
try:
print("Trying encoder: " + encoders[encode_index])
data = open_file(file_in, encoders[encode_index], delimination)
except:
continue
print("")
encoder = encoders[encode_index]
else:
encoder = "Could not auto determine encoder type."
return encoder
def encoding_selection(statement):
basic_encoders = ['utf_8', 'latin1', 'utf_16', 'See All Encoders']
advanced_encoders = ['ascii', 'big5', 'big5hkscs', 'cp037', 'cp424',
'cp437', 'cp500', 'cp720', 'cp737', 'cp775',
'cp850', 'cp852', 'cp855', 'cp856', 'cp857',
'cp858', 'cp860', 'cp861', 'cp862', 'cp863',
'cp864', 'cp865', 'cp866', 'cp869', 'cp874',
'cp875', 'cp932', 'cp949', 'cp950', 'cp1006',
'cp1026', 'cp1140', 'cp1250', 'cp1251', 'cp1252',
'cp1253', 'cp1254', 'cp1255', 'cp1256', 'cp1257',
'cp1258', 'euc_jp', 'euc_jis_2004', 'euc_jisx0213', 'euc_kr',
'gb2312', 'gbk', 'gb18030', 'hz', 'iso2022_jp',
'iso2022_jp_1', 'iso2022_jp_2', 'iso2022_jp_2004', 'iso2022_jp_3', 'iso2022_jp_ext',
'iso2022_kr', 'latin_1', 'iso8859_2', 'iso8859_3', 'iso8859_4',
'iso8859_5', 'iso8859_6', 'iso8859_7', 'iso8859_8', 'iso8859_9',
'iso8859_10', 'iso8859_11', 'iso8859_13', 'iso8859_14', 'iso8859_15',
'iso8859_16', 'johab', 'koi8_r', 'koi8_u', 'mac_cyrillic',
'mac_greek', 'mac_iceland', 'mac_latin2', 'mac_roman', 'mac_turkish',
'ptcp154', 'shift_jis', 'shift_jis_2004', 'shift_jisx0213', 'utf_32',
'utf_32_be', 'utf_32_le', 'utf_16', 'utf_16_be', 'utf_16_le',
'utf_7', 'utf_8', 'utf_8_sig']
while True:
try:
print()
print(statement)
for j, i in enumerate(basic_encoders):
if j != len(basic_encoders) - 1:
print(str(j) + ": to use " + str(i) + "")
else:
print(str(j) + ": to see all possible encoders.")
encoder = basic_encoders[int(input("Enter Selection: "))]
except ValueError:
print("Input must be integer between 0 and " + str(len(basic_encoders)))
continue
else:
break
if encoder == 'See All Encoders':
while True:
try:
print()
print(statement)
for j, i in enumerate(advanced_encoders):
print(str(j) + ": to use " + str(i) + "")
encoder = advanced_encoders[int(input("Enter Selection: "))]
except ValueError:
print("Input must be integer between 0 and " + str(len(advanced_encoders)))
continue
else:
break
print()
return encoder
def open_file(file_in, encoder, delimination):
try:
data = pd.read_csv(file_in, low_memory=False, encoding=encoder, delimiter=delimination)
print("Opened file using encoder: " + encoder)
except UnicodeDecodeError:
print("Encoder Error for: " + encoder)
return "Encode Error"
return data
def select_file_in():
# Hide Tkinter GUI
Tk().withdraw()
file_in = askopenfilename(initialdir="../", title="Select file",
filetypes=(("Comma Separated Values", "*.csv"), ("all files", "*.*")))
if not file_in:
input("Program Terminated. Press Enter to continue...")
exit()
return file_in
def select_file_out_kml(file_in):
# Hide Tkinter GUI
Tk().withdraw()
file_out = asksaveasfilename(initialdir=file_in, title="Select file",
filetypes=(("Keyhole Markup Language", "*.kml"), ("all files", "*.*")))
if not file_out:
input("Program Terminated. Press Enter to continue...")
exit()
# If file doesn't include .kml, add it
if file_out[-4:].lower() != ".kml":
file_out = file_out + ".kml"
else:
# Replace the extension with all lower case
file_out = file_out[:-4] + ".kml"
# Create an empty output file
open(file_out, 'a').close()
return file_out
def select_file_out_csv(file_in):
# Hide Tkinter GUI
Tk().withdraw()
file_out = asksaveasfilename(initialdir=file_in, title="Select file",
filetypes=(("Comma Separated Values", "*.csv"), ("all files", "*.*")))
if not file_out:
input("Program Terminated. Press Enter to continue...")
exit()
# If file doesn't include .kml, add it
if file_out[-4:].lower() != ".csv":
file_out = file_out + ".csv"
else:
# Replace the extension with all lower case
file_out = file_out[:-4] + ".csv"
# Create an empty output file
open(file_out, 'a').close()
return file_out
def delete_file(file_path):
if os.path.exists(file_path):
try:
os.remove(file_path)
return True
except:
print("File could not be deleted. This may be a permission issue or the file is currently open!")
return False
else:
print("The file does not exist, thus could not be deleted!")
return True
def select_multiple_files(title, file_type):
if file_type == 'txt':
file_type_string = "Text"
else:
file_type_string = "Comma Separated Values"
files_in = askopenfilenames(initialdir="../", title=title, filetypes=((file_type_string, "*." + file_type),
("all files", "*.*")))
if not files_in:
return None
else:
return files_in