-
Notifications
You must be signed in to change notification settings - Fork 0
/
reviews.py
65 lines (46 loc) · 1.75 KB
/
reviews.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
import pandas as pd, os
from datetime import datetime
from tripadvisor_reviews import run_trpdvsr
from google_reviews import run_ggl
def download_reviews():
try:
run_ggl()
run_trpdvsr()
except:
print("something goes wrong")
path = os.getcwd()
dirs = os.listdir(path)
csv_list = [file for file in dirs if file.endswith('.csv')]
def clean(dataframe):
#Drop empty comment from cells
cleaned_df = dataframe[dataframe["comment"].str.contains("Text Null") == False]
cleaned_df.reset_index(drop=True, inplace=True)
row_count = len(cleaned_df)
print(f'Your csv has {row_count} rows.')
return cleaned_df
#Checking if only you want handle 1 csv
if "empty_list.csv" in csv_list:
for csv in csv_list:
if csv != 'empty_list.csv':
# only 1 file
df = pd.read_csv(csv)
new_df = df[['comment', 'date']].copy()
clean_df = clean(new_df)
date_object = datetime.now().date()
date_str = date_object.strftime("%Y_%m_%d")
return clean_df.to_csv(f'{date_str}_export.csv', sep=';')
else:
flag = len(csv_list)
if flag == 2:
frames = []
for csv in csv_list:
df = pd.read_csv(csv)
new_df = df[['comment','date']].copy()
frames.append(new_df)
result = pd.concat(frames)
clean_df = clean(result)
date_object = datetime.now().date()
date_str = date_object.strftime("%Y_%m_%d")
return clean_df.to_csv(f'_{date_str}_export.csv', sep=';')
else:
raise ValueError("You have more than 2 csv file to concat")