开发者

Convert \r text to \n so readlines() works as intended

开发者 https://www.devze.com 2022-12-12 12:55 出处:网络
In Python, you can read a file and load its lines into a list by using f = open(\'file.txt\',\'r\') lines = f.readlines()

In Python, you can read a file and load its lines into a list by using

f = open('file.txt','r')
lines = f.readlines()

Each individual line is delimited by \n but if the contents of a line have \r then it is not treated as a new line. I need to convert all \r开发者_运维百科 to \n and get the correct list lines.

If I do .split('\r') inside the lines I'll get lists inside the list.

I thought about opening a file, replace all \r to \n, closing the file and reading it in again and then use the readlines() but this seems wasteful.

How should I implement this?


f = open('file.txt','rU')

This opens the file with Python's universal newline support and \r is treated as an end-of-line.


If it's a concern, open in binary format and convert with this code:

from __future__ import with_statement

with open(filename, "rb") as f:
    s = f.read().replace('\r\n', '\n').replace('\r', '\n')
    lines = s.split('\n')
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号