forked from SidWho88/TEA_Twitter-Extraction-and-Analysis
-
Notifications
You must be signed in to change notification settings - Fork 0
/
common_utils.py
53 lines (45 loc) · 1.71 KB
/
common_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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
import csv
import os
import sys
class FastWriter:
def __init__(self):
self._status = ""
self._csv_file = ""
self._writer = ""
self._db_file = ""
def fast_writer(self, f_name, text_dict):
try:
# Windows
if os.name == "nt":
with open(f_name, 'w', newline='') as self._csv_file:
self._writer = csv.writer(self._csv_file)
for self._key, self._value in text_dict.items():
self._writer.writerow([self._key, self._value])
# Unix/Linux
else:
with open(f_name, 'w') as self._csv_file:
self._writer = csv.writer(self._csv_file)
for self._key, self._value in text_dict.items():
self._writer.writerow([self._key, self._value])
except Exception as e:
print("[ERROR] Unable to write file on disk. Exit...")
print("\n[Details]: ", e)
sys.exit()
def backup_db(self, elem, fout):
try:
if os.name == "nt":
with open(fout, 'a', newline='') as self._db_file:
self._writer = csv.writer(
self._db_file
)
self._writer.writerow(elem.split(","))
else:
with open(fout, 'a') as self._db_file:
self._writer = csv.writer(
self._db_file
)
self._writer.writerow(elem.split(","))
except Exception as e:
print("[ERROR] Unable to write file on disk. Exit...")
print("\n[Details]: ", e)
sys.exit()