How to split a python string on new line characters

前端 未结 2 751
北荒
北荒 2020-12-03 06:22

In python3 in Win7 I read a web page into a string.

I then want to split the string into a list at newline characters.

I can\'t enter the newline into my cod

2条回答
  •  無奈伤痛
    2020-12-03 07:03

    a.txt

    this is line 1
    this is line 2
    

    code:

    Python 3.4.0 (default, Mar 20 2014, 22:43:40) 
    [GCC 4.6.3] on linux
    Type "help", "copyright", "credits" or "license" for more information.
    >>> file = open('a.txt').read()
    >>> file
    >>> file.split('\n')
    ['this is line 1', 'this is line 2', '']
    

    I'm on Linux, but I guess you just use \r\n on Windows and it would also work

提交回复
热议问题