📄 polgensql.py
字号:
#
# Oct 29, 2002
# The program has been modified. You can now use the windows executable file to convert your
# MARC to phpmylibrary sql compatible data.
# After you converted it you can now export it to your phpmylibrary database.
# To use windows simply type polGenSQL somemarc.txt --> then it will generate phpmylibrary.sql
# simply export the phpmylibrary.sql into your database.
#
# Thank you for using PhpMyLibrary. To use this application kindly
# download the Python Software at http://www.python.org
# and MySQLdb: a Python interface for MySQL
#
#
#
# To use this software simply type in linux or windows with python installed:
#
# python polGenSQL.py somemarc.txt
#
# When this script is finished it will
# generate a file called phpmylibrary.sql
# This file can now be exported to your
# phpmylibrary database.
#
# Polerio Babao III
# http://phpmylibrary.sourceforge.net
#
# Please proceed to your PhpMyLibrary admin page for more instruction.
# The admin page has a link to polGenSQL. Please browse through that
# first. Before you could execute the script be sure to generate from
# your PhpMyLibrary admin page the desired numbers you will need.
# If your database doesn't have any data, you should put a values of
# 13 times separated by space then next is the filename.
# ex. python polGenSQL.py 1 1 1 1 1 1 1 1 1 1 1 1 1 marc.txt
# The marc.txt is the filename. Do specify your marc filename there instead
# of marc.txt.
# ----------------------------------------------------------------
# Notes taken from your phpmylibrary admin page.!
#
# polGenSQL.py - MARC to SQL format conversion for export into
# your PhpMyLibrary software. Coded by Polerio Babao III for use in you MARC.
# Accessed it at your directory PhpMyLibrary/polerio/python/ directory.
#
# Execute this file into your computer to generate sql files.
# This file will be used to export your MARC data into your
# phpmylibrary database.
#
#
# $ python polGenSQL.py 24 79 24 14 24 23 23 23 23 5 21 14 23 marc.txt
#
# Numbers generated above came from your database. Those numbers will be used by
# polGenSQL.py file to generate SQL files out of your MARC data.
#
# Don't modify it. Just proceed into your directory where polGenSQL.py and your MARC
# data resides. Then run it. It will generate phpmylibrary.sql files. This file can be
# now be imported by your database. It requires Python and Mysqldb module.
#
# polGenSQL.py Polerio Babao III polerio@users.sourceforge.net
#
# ----------------------------------------------------------------
#
# The mode has changed. The PhpMyLibrary is non-auto increment db
# I can now create time and random for my ids.
# Oct 24, 2002
import sys, string, time
cmd = raw_input('\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n^_^ Format: polGenSQL [input file] [output file] \n\ninput file - the name of the marc file to be converted. \noutput file - the file in which the converted MARC will be saved!\n\nPlease re-check your parameters.\n\ni.e c:\python22\python polGenSQL.py anymarcfile.txt phpmylibrary1.sql (if you have python)\n\ni.e. $ python polGenSQL.py somemarc.txt phpmylibrary1.sql (if you\'re in linux with python.)\n\nYou can use the executable polGenSQL.exe in Windows\n\nTo use simply type polGenSQL somemarc.txt phpmylibrary1.sql\n\nIf you didn\'t follow this combination\nJust press Ctrl-C\n\nContinue? Press Any Key\n\n\n')
def timeandrandom():
a = '%s' % round(time.time())
b = '%s' % time.clock()
c = '%s%s' % (a[0:10],b[2:10])
return c
# time.clock()
# time.time()
# a = '%s' % time.clock()
# c = a[1:4]
# int(c)
file = sys.argv[1]
file2 = sys.argv[2]
def gettimenumber():
a = string.replace(str(time.time()),'.','')
b = string.replace(str(time.clock()),'.','')
anyid = '%s%s' % (a[0:10],b[0:8])
return anyid
def saveanyidx(tags,values):
values = string.replace(values,'[from old catalog]',' ')
values = string.replace(values,',',' ')
values = string.replace(values,'.',' ')
values = string.replace(values,'/',' ')
values = string.replace(values,';',' ')
values = string.replace(values,'-',' ')
tsp = string.split(values,'$')
c=''
for i in range(len(tsp)-1):
s = tsp[i+1]
c = c + ' ' + s[1:]
return c
log = open(file2,'w')
text = open(file, 'r')
content = text.read()
content = string.replace(content,chr(34),'')
content = string.replace(content,chr(39),'')
content = string.replace(content,'\n','')
content = string.replace(content,chr(92),'')
content = string.replace(content,chr(13),'')
split_tn = string.split(content,chr(29))
total_marc = content.count(chr(29))
rec = 0
fld = 0
mz = len(split_tn)
pointer = 1
for x in split_tn:
if pointer <= (mz-1):
print 'Exporting record %s of %s.' % (pointer,mz-1)
pointer = pointer + 1
split_th = string.split(x,chr(30))
dummy = gettimenumber()
bibid = gettimenumber()
holid = gettimenumber()
dd = holid[12:]
log.write('# Record no.%s of %s.\n' % (pointer-1,mz-1))
quer = """insert into _tblbib values (%s,'%s',1,1,%s);""" % (bibid,x,bibid)
log.write('%s\n' % quer)
quer = """insert into _holdings values (%s,%s,%s,1,'JSHS-%s',1);""" % (holid,bibid,bibid,dd)
log.write('%s\n' % quer)
fld = 0
p = 0
q = 3
for y in split_th:
split_to = string.split(y,chr(31))
if fld == 0:
tag = y[24:]
else:
y = string.replace(y,chr(31),'$')
tags = tag[p:q]
if tags == '100':
anyid = gettimenumber()
any = saveanyidx(tags,y)
quer = """insert into _tblanyindex values (%s,'%s');""" % (anyid,any)
log.write('%s\n' % quer)
quer = """insert into _tblanybibrelation values (%s,%s,%s);""" % (anyid,anyid,bibid)
log.write('%s\n' % quer)
quer = """insert into _tblauthorindex values (%s,'%s');""" % (anyid,any)
log.write('%s\n' % quer)
quer = """insert into _tblauthorbibrelation values (%s,%s,%s);""" % (anyid,anyid,bibid)
log.write('%s\n' % quer)
elif tags == '245':
anyid = gettimenumber()
any = saveanyidx(tags,y)
quer = """insert into _tblanyindex values (%s,'%s');""" % (anyid,any)
log.write('%s\n' % quer)
quer = """insert into _tblanybibrelation values (%s,%s,%s);""" % (anyid,anyid,bibid)
log.write('%s\n' % quer)
quer = """insert into _tbltitleindex values (%s,'%s');""" % (anyid,any)
log.write('%s\n' % quer)
quer = """insert into _tbltitlebibrelation values (%s,%s,%s);""" % (anyid,anyid,bibid)
log.write('%s\n' % quer)
elif tags == '260':
anyid = gettimenumber()
any = saveanyidx(tags,y)
quer = """insert into _tblanyindex values (%s,'%s');""" % (anyid,any)
log.write('%s\n' % quer)
quer = """insert into _tblanybibrelation values (%s,%s,%s);""" % (anyid,anyid,bibid)
log.write('%s\n' % quer)
quer = """insert into _tblpublisherindex values (%s,'%s');""" % (anyid,any)
log.write('%s\n' % quer)
quer = """insert into _tblpublisherbibrelation values (%s,%s,%s);""" % (anyid,anyid,bibid)
log.write('%s\n' % quer)
elif tags == '500':
anyid = gettimenumber()
any = saveanyidx(tags,y)
quer = """insert into _tblanyindex values (%s,'%s');""" % (anyid,any)
log.write('%s\n' % quer)
quer = """insert into _tblanybibrelation values (%s,%s,%s);""" % (anyid,anyid,bibid)
log.write('%s\n' % quer)
quer = """insert into _tblnotesindex values (%s,'%s');""" % (anyid,any)
log.write('%s\n' % quer)
quer = """insert into _tblnotesbibrelation values (%s,%s,%s);""" % (anyid,anyid,bibid)
log.write('%s\n' % quer)
elif tags == '630':
anyid = gettimenumber()
any = saveanyidx(tags,y)
quer = """insert into _tblanyindex values (%s,'%s');""" % (anyid,any)
log.write('%s\n' % quer)
quer = """insert into _tblanybibrelation values (%s,%s,%s);""" % (anyid,anyid,bibid)
log.write('%s\n' % quer)
quer = """insert into _tblsubjectindex values (%s,'%s');""" % (anyid,any)
log.write('%s\n' % quer)
quer = """insert into _tblsubjectbibrelation values (%s,%s,%s);""" % (anyid,anyid,bibid)
log.write('%s\n' % quer)
elif tags == '650':
anyid = gettimenumber()
any = saveanyidx(tags,y)
quer = """insert into _tblanyindex values (%s,'%s');""" % (anyid,any)
log.write('%s\n' % quer)
quer = """insert into _tblanybibrelation values (%s,%s,%s);""" % (anyid,anyid,bibid)
log.write('%s\n' % quer)
quer = """insert into _tblsubjectindex values (%s,'%s');""" % (anyid,any)
log.write('%s\n' % quer)
quer = """insert into _tblsubjectbibrelation values (%s,%s,%s);""" % (anyid,anyid,bibid)
log.write('%s\n' % quer)
p = p + 12
q = q + 12
fld = fld + 1
rec = rec + 1
print '\n\nDone! Thank you for using Phpmylibrary'
print '\n\nOpen your the output file to see converted SQL Or.'
print '\n\nJust export it into your database.'
print '\n\nEx. mysql -uroot -hlocalhost -p phpmylibrarydb < phpmylibrary.sql'
print '\n\nPolerio Babao polerio@users.sourceforge.net'
print '\nhttp://phpmylibrary.sourceforge.net'
print '\n\n^_^ Thank your for your interest in PhpMyLibrary\n\n\n'
print 'Created at PhpMyLibrary-2.0.2 Oct 30, 2002\n\n\n\n'
cmd = raw_input('Press any key to exit!.')
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -