2017-11-27 14 views
1

여기서 내가 잘못하고있는 것이 확실하지 않습니까? \를 특수 문자로 처리해야합니까? 문자열 \ r \ n은 문자 그대로 txt 파일의 문자열에 나타납니다./r/n 하위 문자열을 찾음으로써 문자열 데이터를리스트 목록으로 나눕니다

def split_into_rows(weather_data): 
    list_of_rows = [] 

    while not (weather_data.find("\r\n") == -1): 
     firstreturnchar = weather_data.find("\r\n") 
     row = weather_data[ :firstreturnchar] 
     list_of_rows = list_of_rows.append(row) 

    return list_of_rows 

내가 거기에 문자열 \ r에 \의 예는 여전히 n은 문자열의 왼쪽 부분 문자열의 첫 번째 인스턴스를 찾는 동안,이다 필요한 것은 "\ 연구 \ n을", 그 전에 모든 것을 잘라 넣어 그것을 변수 행에 넣은 다음, 행을 list_of_rows에 추가하십시오.

+1

'list_of_rows = list_of_rows.append (행) ':이 목록은 당신이 weather_data.splitlines'를 통해 단지 그렇게 할 수 변경할 수 – Mangohero1

+2

참고하며, 할당을 필요로하지 않습니다()' – GPhilo

+0

백 슬래시를 이스케이프 처리해야합니다 (앞에서 다른 백 슬래시를 입력하십시오). '\ r'과'\ n'는 모두 특수 문자입니다. [이 비슷한 질문.] (https://stackoverflow.com/questions/11331778/find-backslash-in-a-string-python) – colopop

답변

1

당신은 split() 사용할 수 있습니다

def split_into_rows(weather_data): 

    return weather_data.split('\\r\\n') 
+1

그건 작동합니다! def_into_rows (weather_data)와 함께 특수 문자를 이스케이프해야한다는 것을 제외하면 다음과 같습니다. return weather_data.split ('\\ r \\ n') – Davtho1983

+1

수락하기 전에 3 분 안에 있습니다! – Davtho1983