- 论坛徽章:
- 0
|
本帖最后由 luoyonghao 于 2015-03-10 09:12 编辑
需求是这样的
我有一个日志文件 里面的内容大概为
[2015-03-09 15:30:31] sta-000.bat dongfang 192.168.1.1
[2015-03-09 15:52:33] sta-000.bat cctv8 192.168.1.3
[2015-03-09 15:52:33] sta-000.bat cctv8 192.168.1.3
[2015-03-09 16:01:02] sta-000.bat cctv8 192.168.1.3
[2015-03-09 16:30:31] sta-000.bat dongfang 192.168.1.1
我想把这里面的内容逐行插入到mysql数据库里,数据库表里有4个字段 分别为city channel time ip
我自己写了个代码- import MySQLdb
- db = MySQLdb.connect("localhost","root","admin","test1" )
- cursor = db.cursor()
- f = open('d:\\run.log')
- for s in f.readlines():
- time = s[1:20]
- channel = s[34:-12]
- city = 'shenyang'
- ip = s[-12:]
- cursor.execute('insert into run_log values("%s","%s","%s","%s")' % (city,channel,time,ip))
- db.commit()
- db.close()
复制代码 结果执行的时候程序报错
Traceback (most recent call last):
File "C:/Users/Administrator/Desktop/log.py", line 12, in <module>
cursor.execute('insert into run_log values ("shenyang","dongfang","2015-03-07 19:20:20","192.168.1.1")')
File "C:\Python27\lib\site-packages\MySQLdb\cursors.py", line 180, in execute
charset = db.character_set_name()
InterfaceError: (0, '')
现在我不知道问题出在哪里,或者哥哥们有什么更好的办法能实现这个功能,不胜感激! |
|