如何使用python将以下信息入库
{ "_id" : ObjectId( "4ad9469045343000008" ), "passwd" : "passwd1", "mail" : "123456@163.com", "id" : "123456@163.com", "src" : "weibo" }
{ "_id" : ObjectId( "4ad9469045343000009" ), "passwd" : "passwd2", "mail" : "123456@254.com", "id" : "123456@254.com", "src" : "weibo" }
文件格式如上,大括号内是一行
现想把passed mail id src 后面的内容 即冒号后的内容 入到mysql里面
字段名为passed mail id src
如何用python实现 注意:密码中会出现双引号、单引号和逗号
[解决办法]
#!/usr/bin/env pythonsfile = ( '{ "_id" : ObjectId( "4ad9469045343000008" ), "passwd" : "passwd1", "mail" : "123456@163.com", "id" : "123456@163.com", "src" : "weibo" }', '{ "_id" : ObjectId( "4ad9469045343000009" ), "passwd" : "passwd2", "mail" : "123456@254.com", "id" : "123456@254.com", "src" : "weibo" }',)import repat = re.compile('{ "_id" : ObjectId\( (.*) \), "passwd" : (.*), "mail" : (.*), "id" : (.*), "src" : (.*) }');for line in sfile: rs = pat.search(line) if rs: t = (rs.group(2), rs.group(3), rs.group(4), rs.group(5)) sql = 'insert into tbl (passed, mail, id, src) values (%s)' % ','.join(t) print sql