-
Notifications
You must be signed in to change notification settings - Fork 13
/
helper_functions.py
83 lines (65 loc) · 1.86 KB
/
helper_functions.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
#!/usr/bin/env python2
# -*- coding: utf-8 -*-
"""
Created on Sun Jun 3 15:47:27 2018
@author: joseramon
"""
from unidecode import unidecode
from urllib import unquote
"""
Request Headers for the GET Request
"""
def save_to_file(dataset, file_path):
"""
Function to save a list of urls into a txt file
"""
with open(file_path, 'w') as f:
for item in dataset:
processed = process_info(item)
f.write(processed+'\n')
def populate_countries_dict(file_path):
"""
Function to populate dictionary of countries and url numbers.
"""
countries_dict = {}
with open(file_path, 'r') as f:
for line in f.readlines():
key_value = line[:-1].split(':')
countries_dict[key_value[0]] = int(key_value[1])
return countries_dict
def read_from_file(file_path):
"""
Read a file and return a list with all the lines in the file
"""
file_in_list = []
with open(file_path, 'r') as f:
for line in f.readlines():
file_in_list.append(line)
return file_in_list
def append_to_tsv(player, file_path):
"""
Function to append new data into a tsv file
"""
with open(file_path, 'a') as f:
f.write('\t'.join(player)+'\n')
def append_to_csv(player, file_path):
"""
Function to append new data into a tsv file
"""
with open(file_path, 'a') as f:
f.write(','.join(player)+'\n')
def append_to_file(line, file_path):
"""
Function to append new data into file
"""
with open(file_path, 'a') as f:
f.write(line+'\n')
def process_info(info):
try:
processed = unquote(info)
except:
processed = info
pass
if isinstance(processed, unicode):
processed = unidecode(processed)
return processed