python - Splitting a string separated by "\r\n" into a list of lines? -


I'm reading some data from the communication method of the subprocess module. I'm a big string divided by "\ r \ n" s Coming as I want to divide it into a list of lines, how is this done in dragon?

Use splitline method on string.

From the docs:

str.splitlines ([keepends]) Return a list of rows in the string, break line breaks on line borders The result is not included in the list, unless the slope is given and is not correct.

This will do the right thing, regardless of the end of the line "\ r \ n", "\ r" or "\ N" OS.

The line ending the NB "\ n \ r" will also be divided, but you will get an empty string between each line, because it will consider "\ n" as a valid line ends And as the end of the next line of "\ r" E.g.

  & gt; & Gt; & Gt; "Foo \ n \ rbar" .splitlines () ['foo', '', 'bar']  

Comments

Popular posts from this blog

c# - sqlDecimal to decimal clr stored procedure Unable to cast object of type 'System.Data.SqlTypes.SqlDecimal' to type 'System.IConvertible' -

Calling GetGUIThreadInfo from Outlook VBA -

Obfuscating Python code? -