2013-06-21 2 views
2

나는 다음과 같은 형식의 라인과 함께 IIS 로그 파일이 dataframe에 IIS 로그를 읽기 :팬더가

61.245.163.59을 - [16// 2013 월 : 23 : 55 : 09 0530] "GET /ehrm/Recruitment/Images/divider.gif HTTP/1.1 "404 1245 "http://www.example.com/ehrm/Recruitment/MyApplication.aspx?PRF_ID=000005&digest=6LL4BTSuW9YnE5R4T8k27Q ""Mozilla/5.0 (Windows NT 6.1; rv : 20.0) Gecko/20100101 Firefox/20.0 "GET/ehrm/Recruitment/Images/divider .gif - HTTP/1.1 www.example.com

여기에서 일부 열을 가져 와서 데이터 프레임을 만들고 싶습니다. 다음과 같은 방법으로 하나의 열만있는 데이터 프레임을 만듭니다. 각 분할 열을 데이터 프레임의 한 열로 만들고 싶습니다. 그리고 다른 것은 로그 파일의 길이가 고유하지 않기 때문에 어떻게 분할하여 값을 취하는 정확도를 향상시킬 수 있을까요?

log_list = [] 
for line in f: 
    ip = (line.split(' ')[0]) 
    time = (line.split(' ')[2]) 
    method = (line.split(' ')[4]) 
    status = (line.split(' ')[7]) 
    bytes = (line.split(' ')[8]) 
    referrer = (line.split(' ')[9]) 
    agent = (line.split(' ')[10]) 
    data = ip + ' ' + time + ' ' + method + ' ' + status + ' ' + bytes + ' ' + referrer + ' ' + agent 
    log_list.append(data) 
df = pandas.DataFrame(log_list) 
+0

[read_csv] (http://pandas.pydata.org/pandas-docs/stable/generated/pandas.io.parsers.read_csv.html#pandas-io-parsers-read-csv) 함수를 사용해 보셨습니까? ? 시간과 같은 열에 문제가있을 수 있지만 그 후에 조합 할 수 있습니다. 기본적으로 구분 기호는 공백임을 확인해야하지만'sep = '\ s +''인수를 전달할 수 있습니다. – TomAugspurger

+0

@TomAugspurger : 고마워! 솔루션을 이해할 수 없기 때문에 분할을 별도의 파일로 작성한 다음 데이터 프레임으로 가져 왔습니다. –

답변

2

다음 코드는 당신이 뭘 하려는지 달성해야

from pandas import read_csv 
log_file = 'filename.log' 
df = read_csv(log_file, sep=r'\s+', usecols=[0, 2, 4, 7, 8, 9, 10]) 

read_csv documentation합니다.

관련 문제