擅长:python、mysql、java
<p>您可以使用<code>str.splitlines</code>:</p>
<pre><code>>>> convo = "Bob: Hello \n Sandy: How are you? \n Bob: Confused by a python problem"
>>> for line in convo.splitlines():
... print(line)
...
Bob: Hello
Sandy: How are you?
Bob: Confused by a python problem
>>>
</code></pre>
<p>从<a href="https://docs.python.org/3/library/stdtypes.html#str.splitlines">docs</a>:</p>
<blockquote>
<p><code>str.splitlines([keepends])</code></p>
<p>Return a list of the lines in the string, breaking at line boundaries. This method
uses the universal newlines approach to splitting lines. Line breaks are not included in the resulting list unless keepends is given and true.</p>
</blockquote>