Python 通过查找/r/n子字符串将字符串数据划分为列表列表

Python 通过查找/r/n子字符串将字符串数据划分为列表列表,python,string,special-characters,python-3.6,Python,String,Special Characters,Python 3.6,我不确定我做错了什么?是否需要将\视为特殊字符?序列\r\n在我使用的txt文件的字符串中出现 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[ :firstreturncha

我不确定我做错了什么?是否需要将\视为特殊字符?序列\r\n在我使用的txt文件的字符串中出现

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”的第一个实例,请切掉前面的所有内容并将其放入变量行中,然后将行追加到行列表中。

您可以使用
split()


list\u of_rows=list\u of_rows.append(row)
:这不需要赋值,列表是可变的。请注意,您可以通过
weather\u data.splitlines()
执行此操作。您需要转义反斜杠(在前面键入另一个反斜杠)<代码>\r和
\n
都是特殊字符。看到了吧!除了我仍然需要将def split_的特殊字符转义为_行(weather_data):返回weather_data.split(“\\r\\n”)
def split_into_rows(weather_data):

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