Split with an empty line separator

I'm having trouble splitting a text file using blank lines "\n\n".

re.split("\n", aString) 

works but

re.split("\n\n", aString) 

just returns the whole string.

Any ideas?

+3
source share
1 answer

Beware of line termination conventions across operating systems !

  • Windows: CRLF ( \r\n)
  • Linux and other Unix: LF ( \n)
  • Old Mac: CR ( \r)

You are probably mistaken because the double new line you are looking for is in a Windows-encoded text file and will display as \r\n\r\n, rather than \n\n.

The function repr()will tell you for sure that you are ending:

>>> mystring = #[[a line of your file]]
>>> repr(mystring)
"'\\nmulti\\nline\\nstring '"

, ?

with open(file.txt, 'r') as f:
    for line in f:
        print (line)
+4

All Articles