日期:2014-05-16  浏览次数:20502 次

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

class 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 string
		sql = "insert into juzhai2(id,age,address,constellation,work) values(" \
									+ person.getid() + ",'" + person.getage() + "','" + person.getaddress() + "','" \
									+ person.getconstellation() + "','" + person.getwork() + "')"
		print sql
		cursor.execute("SET NAMES 'gbk'")
		cursor.execute(sql)
		# Fetch all results from the cursor into a sequence and close the connection
		results = 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 match

def parse_file(afile):
	pass

reload(sys)
sys.setdefaultencoding('utf-8')  

print sys.getdefaultencoding()
# Create a connection object, then use it to create a cursor
con = 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 e
		continue
	finally:
		file_object.close( )

con.commit()
con.close( )