1

I'm not sure what I'm doing wrong here? Do I need to treat the \ as a special character? The sequence \r\n appears literally in the string in the txt file I am using.

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

What I need is, while there are still examples of the substring \r\n left in the string, to find the first instance of the substring "\r\n", chop everything before that and put it into the variable row, then append row to list_of_rows.

Davtho1983
  • 3,827
  • 8
  • 54
  • 105
  • 1
    `list_of_rows = list_of_rows.append(row)`: this doesn't require an assignment, lists are mutable – Mangohero1 Nov 27 '17 at 15:18
  • 2
    Note that you can do just that via `weather_data.splitlines()` – GPhilo Nov 27 '17 at 15:18
  • You need to escape the backslash (type another backslash in front). `\r` and `\n` are both special characters. See [this similar question.](https://stackoverflow.com/questions/11331778/find-backslash-in-a-string-python) – colopop Nov 27 '17 at 15:20

1 Answers1

1

You could use split():

def split_into_rows(weather_data):

    return weather_data.split('\\r\\n')
Reblochon Masque
  • 35,405
  • 10
  • 55
  • 80
  • 1
    That works! except I still need to escape the special char with def split_into_rows(weather_data): return weather_data.split('\\r\\n') – Davtho1983 Nov 27 '17 at 15:22