首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 数据库 > 其他数据库 >

python脚本处置文件到数据库

2012-08-25 
python脚本处理文件到数据库#!/usr/bin/python# -*- coding:gb2312 -*- import glob, os, MySQLdb, syscla

python脚本处理文件到数据库

#!/usr/bin/python# -*- coding:gb2312 -*- import glob, os, MySQLdb, sysclass M(type):      def __new__(cls, name, bases, classdict):          for attr in classdict.get('__slots__', ( )):              if attr.startswith('_'):                  def getter(self, attr=attr):                       return getattr(self, attr)                  def setter(self, val=0, attr=attr):                      return setattr(self, attr, val)                  classdict['get' + attr[1:]] = getter                  classdict['set' + attr[1:]] = setter          return type.__new__(cls, name, bases, classdict)def save_person(con, person):try:cursor = con.cursor( )# Execute an SQL stringsql = "insert into juzhai2(id,age,address,constellation,work) values(" \+ person.getid() + ",'" + person.getage() + "','" + person.getaddress() + "','" \+ person.getconstellation() + "','" + person.getwork() + "')"print sqlcursor.execute("SET NAMES 'gbk'")cursor.execute(sql)# Fetch all results from the cursor into a sequence and close the connectionresults = cursor.fetchall( )finally:cursor.close()class Person(object):      __metaclass__ = M      __slots__ = ['_id', '_age' ,'_address','_constellation','_work'] def all_files(pattern, search_path, pathsep=os.pathsep):    """ Given a search path, yield all files matching the pattern. """    for path in search_path.split(pathsep):        for match in glob.glob(os.path.join(path, pattern)):            yield matchdef parse_file(afile):passreload(sys)sys.setdefaultencoding('utf-8')  print sys.getdefaultencoding()# Create a connection object, then use it to create a cursorcon = MySQLdb.connect(host="127.0.0.1", port=3306,     user="root", passwd="ning", db="test",charset='utf8')files = all_files("*.txt",r"D:\program\juzhai");for f in files:id = f.split('\\')[-1].split('.')[0]file_object = open(f)try:all_the_text = file_object.read( ).split(' ')p = Person()p.setid(id)p.setage(all_the_text[0])p.setaddress(all_the_text[1])p.setconstellation(all_the_text[2])p.setwork(all_the_text[3])save_person(con, p)except IndexError, e:print '/nSome error/exception occurred.'print econtinuefinally:file_object.close( )con.commit()con.close( )

热点排行