こちらのソースコードを参考にapacheのlogをDBに保存しようとしたのですが、ログでbytesの部分が"-"になっている時に下記のエラーが出てきてしまいます。どうしたらいいですか?
ValueError: invalid literal for int() with base 10: '-'
lang
1#coding:utf8 2 3from sqlobject import * 4import sys,csv, sqlite3, datetime 5 6logname = "short_access_log" 7 8class ApacheDialect(csv.Dialect): 9 delimiter = " " 10 doublequote = True 11 lineterminator="\r\n" 12 quotechar='"' 13 quoting=0 14 skipinitialspace=False 15 16class ApacheLog(SQLObject): 17 ip = StringCol() 18 remotelog = StringCol() 19 remoteuser= StringCol() 20 time = DateTimeCol() 21 request = StringCol() 22 status = IntCol() 23 bytes = IntCol() 24 referer = StringCol() 25 ua = StringCol() 26 27 def _set_time(self,timestr): 28 self._SO_set_time( datetime.datetime.strptime(timestr,"[%d/%b/%Y:%H:%M:%S") ) 29 30if __name__ == '__main__': 31 32 sqlhub.processConnection = connectionForURI('sqlite:///tmp/abc.db') 33 ApacheLog.createTable(ifNotExists=True) 34 35 csv.register_dialect("apache",ApacheDialect) 36 reader = csv.reader(open(logname),dialect="apache") 37 38 for r in reader: 39 ApacheLog( 40 ip = r[0], 41 remotelog = r[1], 42 remoteuser= r[2], 43 time = r[3], 44 #tz = r[4], 45 request = r[5], 46 status = int(r[6]), 47 bytes = int(r[7]), 48 referer = r[8], 49 ua = r[9] 50 ) 51
この場合のエラーメッセージは
Traceback (most recent call last):
File "log2db.py", line 46, in <module>
bytes = int(r[7]),
ValueError: invalid literal for int() with base 10: '-'
です。
参考HP変更しました。
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2015/06/24 23:25
2015/06/25 12:19 編集
2015/06/29 04:52
2015/06/29 06:13