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( )
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值