件名の通りです。対処方法をご教授いただけませんでしょうか?よろしくお願いいたします。
環境
- Windows10
- Spyder(Python 3.6)
- PostgresSQL 9.4
エラー内容
log
1 22018-11-27 13:53:24 JST ERROR: 列"b"のデータがありません 32018-11-27 13:53:24 JST コンテキスト: access_logのCOPY。行番号 2: "hoge.ne.jp - - [01/Sep/2018:02:00:03 +0900] "GET index.php_..." 42018-11-27 13:53:24 JST ステートメント: COPY access_log FROM STDIN WITH CSV DELIMITER E'\t' header null ''; 5
バッチファイル
bat
1 2call .\parameters-windows.bat 3%PSQL% -f load.sql 4pause 5 6rem parameters-windows.bat 7set PGHOST=localhost 8set PGPORT=5432 9set PGDATABASE=postgres 10set PGUSER=postgres 11set PSQL="C:\Program Files\PostgreSQL\9.4\bin\psql" 12 13
SQL
sql
1 2\encoding utf8 3 4drop table access_log; 5\i src/access_log.ct 6\copy access_log from 'src/access_log.bz2.tsv' WITH CSV DELIMITER E'\t' header null ''; 7
ct
1create table access_log 2( a text 3, b text 4, c text 5, d text 6, e text 7, f text 8, g text 9, h text 10, i text 11, j text 12, k text 13);
Apacheのログ
conf
1"%h %l %u %t \"%r\" %>s %b \"%{Referer}i\" \"%{User-Agent}i\""
Pythonスクリプト
python
1 2import bz2 3import re 4import codecs 5 6fr = bz2.open(r'C:\Users\user\Desktop\bz2\access_log.bz2', 'rt') 7fw = codecs.open(r'C:\Users\user\Desktop\bz2\access_log.bz2.tsv', 'w', encoding='utf-8') 8 9line = fr.readline() 10#line = line.replace(',', ' ') 11 12while line: 13 fline = re.sub(r'^(\S+) (\S+) (\S+) (\S+) [([^]]+)] "([A-Z]+) ([^ "]+)? HTTP/[0-9.]+" ([0-9]{3}) ([0-9]+|-) "([^\"]*)" "([^\"]*)"', r'\1\t\2\t\3\t\4\t\5\t\6\t\7\t\8\t\9\t\10\t\11', line) 14 check = re.search(r'/public/|/bot/|Googlebot|msnbot|bingbot|applebot|SemrushBot|AhrefsBot|MJ12bot|AdsBot|DotBot|istellabot|Twitterbot|YandexMobileBot|/bots|/bot.html|robots.txt', fline) 15 if check is None: 16 fw.write(fline) 17 line = fr.readline() 18fr.close() 19fw.close() 20 21
コピペ & 参考にさせていただいたページ
- PYTHONでAPACHEアクセスログをCSVファイルへ変換する方法
- Apacheのログ出力の要点
- [How to write UTF-8 in a CSV file
](https://stackoverflow.com/questions/18766955/how-to-write-utf-8-in-a-csv-file)
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
退会済みユーザー
2018/11/27 08:52
2018/11/27 09:00
2018/11/27 09:02 編集
退会済みユーザー
2018/11/27 09:04
退会済みユーザー
2018/11/28 06:31