python中回车怎么表示_如何在python中使用读取行仅拆分回车符?
I have a text file that contains both \n and \r\n end-of-line markers. I want to split only on \r\n, but can't figure out a way to do this with python's readlines method. Is there a simple workaround for this?
解決方案
As @eskaev mentions, you'll usually want to avoid reading the complete file into memory if not necessary.
io.open() allows you to specify a newline keyword argument, so you can still iterate over lines and have them split only at the specified newlines:
import io
for line in io.open('in.txt', newline='\r\n'):
print repr(line)
Output:
u'this\nis\nsome\r\n'
u'text\nwith\nnewlines.'
總結
以上是生活随笔為你收集整理的python中回车怎么表示_如何在python中使用读取行仅拆分回车符?的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 余额宝放一万安全吗?从两方面来看!
- 下一篇: 孩子学python用什么教材比较好-py