-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathextract_database.py
66 lines (51 loc) · 1.82 KB
/
extract_database.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
import pymysql
import pandas as pd
db = pymysql.connect("localhost","root","12343249","sparsh" )
cursor = db.cursor()
def ex_questions_answers(query,file_path):
sql = query
try:
cursor.execute(sql)
data = cursor.fetchall()
db.commit()
except:
db.rollback()
#
# file_ques = open('D:/ML/QNA_project/text_files/questions.txt','w')
#
# for i in range(len(data)):
# d = ('{}. '.format(i + 1) + data[i][0]).encode('utf-8')
# file_ques.write(str(d))
# file_ques.write('\n')
#
# file_ques.close()
#
o_d = [data[i][0] for i in range(len(data))]
df = pd.DataFrame(o_d,columns=['Answers'])
df.to_csv(file_path)
def extract_keywords_filter(query , file_path):
sql = query
try:
cursor.execute(sql)
data = cursor.fetchall()
db.commit()
except:
db.rollback()
o_d = [data[i] for i in range(len(data))]
df = pd.DataFrame(o_d,columns=['Entity','Keywords'])
df.to_csv(file_path)
def ex_view_count(query,file_path):
sql = query
try:
cursor.execute(sql)
data = cursor.fetchall()
db.commit()
except:
db.rollback()
df = pd.DataFrame(data, columns=['ID', 'Answers', 'question_id', 'modified_on', 'upvote_count', 'comment_count'])
df.to_csv(file_path)
# ex_questions_answers('select text from sparsh.question_answers','D:/ML/QNA_project/CSV_files/answers.csv')
# ex_questions_answers('select title from sparsh.questions','D:/ML/QNA_project/CSV_files/answers.csv')
# extract_keywords_filter('select entity_type , keyword from sparsh.keywords ','D:/ML/QNA_project/CSV_files/keywords.csv')
ex_view_count('select id ,text,question_id,modified_on , upvote_count , comment_count from sparsh.question_answers ' , 'D:/ML/QNA_project/CSV_files/answers.csv')
db.close()