2014-12-18 2 views
-1
class mainprogram(): 
def getData(self, file, begin, end): 
    parser = MyHTMLParser() 
    f = open(file); 
    rawcontent = f.read() 

    #Get main content 
    content = rawcontent.split('<div id="header"')[1]; 
    content = content.split('</html>')[0]; 

    del parsed_data[:] 
    html = content.split(begin)[1]; 
    html = html.split(end)[0]; 
    parser.feed(html); 
    result = list(parsed_data); 
    return result; 

내가 갇혀 있었다 파이썬과 관련된 과제를 수행하는 동안 나는 파이썬을 실행의 단계에있어 및 사용. 위 코드는 htmlparser를 사용하여 .msg 파일을 구문 분석하여 CSV 형식으로 변환합니다. 어느 한 나에게 설명 할 수 [1] [0] 현재 내가 강조 할 때, 개발을위한 파이썬 커뮤니티 버전을 사용하고 라인 중요성 : HTML 구문 분석 파이썬

content = rawcontent.split('<div id="header"')[1]; 
content = content.split('</html>')[0]; 

아래 이들에 의미 않습니다 어떤 특정 [1] 또는 [0] 그것

special method names 한 바와 같이
class list 

def __getitem__(self, y) 
+1

'list [index]'는'list'에서'index' 위치의 항목을 가져옵니다. 이 기능은'__getitem __ (self, index)'함수로 구현됩니다. 기본적으로 rawcontent.split ('') [0]'은 rawcontent를 문자열 목록으로 분리 한 다음 첫 번째 값 ('[0]')을 검색합니다. 그 질문에 대답합니까? –

답변

0

으로 보이고, 방법 이름 __getitem__() 클래스가 foo[bar] 구문을 대체 할 수있다. 리스트는 서브 스크립팅 (예 : foo[5])과 슬라이싱 (foo[1:5])을 모두 제공하기 위해이 작업을 수행합니다. 사전은 키 조회를 제공하기 위해이 작업을 수행합니다.