37,720
社区成员
发帖
与我相关
我的任务
分享
#!/usr/bin/python
j=open('J_Entrez1.txt')
while True:
t=j.readline().strip().split(':')
if not t:
break
col,value = t
query = "insert into test (`%s`) values (%s)" %(col,'"'+value+'"')
print query
j.close()
#!/usr/bin/python
f = open('J_Entrez2.txt')
LIST_col = []
LIST_value = []
count=0
while True:
# count = 0
line = f.readline().strip().split(':')
if line == ['']:
break
col,value = line
LIST_col.append(col)
LIST_value.append('"'+value+'"')
# query = "insert into test (`%s`) values (%s)" %(col,'"'+value+'"')
count += 1
if count==7:
query = "insert into test (%s) values (%s)" %(','.join(LIST_col),','.join(LIST_value))
print query
count=0
LIST_col = []
LIST_value = []
f.close()
root@zhoujy:/home/zhoujy/0225# python e.py
insert into test (JrId,JournalTitle,MedAbbr,ISSN,ESSN,IsoAbbr,NlmId) values (" 1"," AADE editors' journal"," AADE Ed J"," 0160-6999",""," AADE Ed J"," 7708172")
insert into test (JrId,JournalTitle,MedAbbr,ISSN,ESSN,IsoAbbr,NlmId) values (" 2"," AANA journal"," AANA J"," 0094-6354",""," AANA J"," 0431420")
insert into test (JrId,JournalTitle,MedAbbr,ISSN,ESSN,IsoAbbr,NlmId) values (" 3"," AARN news letter"," AARN News Lett"," 0001-0197",""," AARN News Lett"," 1251052")
root@zhoujy:/home/zhoujy/0225#
fin = open('in.txt','r')
fout = open('out.txt','w')
i=0
out = 'insert into ('
values = ')values('
for line in fin.readlines():
print line
content = line.split(':')
if i % 7 == 6:
out+=content[0].strip()
values+='"' + content[1].strip() + '"'
fout.write(out+values+')\n')
print i
out = 'insert into ('
values = ')values('
else:
out +=content[0].strip()+','
values+='"' + content[1].strip() + '"' + ','
i+=1
fin.close()
fout.close()
fields = "JrId,JournalTitle,MedAbbr,ISSN,ESSN,IsoAbbr,NlmId"
field_list = fields.split(',')
def dict2cmd(record):
values = [record[f] for f in field_list]
return 'insert into ( %s ) values ( "%s" )' % (fields, '","'.join(values))
def file2cmd(f):
counter = 0
record = {}
for line in open(f):
f, v = line.split(":")
record[f.strip()] = v.strip()
counter += 1
if counter == 7:
print dict2cmd(record)
record = {}
counter = 0
import sys
file2cmd(sys.argv[1])
lines = open('J_Entrez1.txt').readlines()
ind = -1
cnt = 0
for line in lines:
if cnt%7==0:
ind += 1
exec('dic'+str(ind)+'={}')
exec('dic'+str(ind)+'[line.strip().split(":")[0]]=line.strip().split(":")[1].strip()')
cnt += 1
for i in range(ind+1):
print '+++++NEW LINE++++++++++++++++++'
first = 'insert into ('
last = ') values('
for key in eval('dic'+str(i)+'.keys()'):
first += key+','
last += '"'+eval('dic'+str(i)+'[key]')+'",'
query = first[:-1]+last[:-1]+')'
print query
>>> ================================ RESTART ================================
>>>
+++++NEW LINE++++++++++++++++++
insert into (MedAbbr,JrId,JournalTitle,ISSN,IsoAbbr,NlmId,ESSN) values("AADE Ed J","1","AADE editors' journal","0160-6999","AADE Ed J","7708172","")
+++++NEW LINE++++++++++++++++++
insert into (MedAbbr,JrId,JournalTitle,ISSN,IsoAbbr,NlmId,ESSN) values("AANA J","2","AANA journal","0094-6354","AANA J","0431420","")
+++++NEW LINE++++++++++++++++++
insert into (MedAbbr,JrId,JournalTitle,ISSN,IsoAbbr,NlmId,ESSN) values("AARN News Lett","3","AARN news letter","0001-0197","AARN News Lett","1251052","")
>>>