-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdata_id_extract.py
47 lines (46 loc) · 1.56 KB
/
data_id_extract.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
import pymysql
import os
db = pymysql.connect("localhost", "root", "*******", "KG_db",charset="utf8",port=3306,cursorclass=pymysql.cursors.Cursor)
cursor = db.cursor()
cursor.execute("use KG_db;")
# sql="SELECT ID FROM Institution WHERE Name = 'Rensselaer Polytechnic Institute'"
# cursor.execute(sql)
# rs=cursor.fetchall()
# for r in rs:
# print(r)
rfile=open("data/data_EachFile.txt","r")
wfile=open("data/data_ID.txt","a")
wfile2=open("wrong.txt","a")
EachMessage=["award","AwardInstrument","Organization","ProgramOfficer","Investigator","Institution","ProgramOfficer","Investigator","Institution","ProgramElement","Directorate","Division"]
result=[]
cnt=0
for line in rfile.readlines():
try:
if line!='\n':
if line[:6]!="SELECT":
result.append(line.strip())
else:
cursor.execute(line)
rs=cursor.fetchall()
# print(rs)
if (len(rs)!=1):
wfile2.write(line)
wfile2.write(str(rs))
wfile2.write("\n\n")
print(line.strip())
print(str(rs))
continue
else:
for r in rs:
result.append(r[0])
else:
cnt+=1
if cnt%100==0:
print(cnt)
wfile.write(str(result)+"\n")
print(result)
result=[]
except:
wfile2.write("wrong\t"+str(line))
wfile2.close()
wfile.close()