Python Tips: How to Read a File Without Newlines

Text files are a common part of data processing.

A standard approach to reading a file is to use the readlines(), but this results in a list with newline characters.

For example, given some sample text and the standard approach:

Hello, World!

Lorem ipsum dolor sit amet, consectetur adipiscing elit.
Sed fermentum justo a dignissim lobortis.
Nullam dapibus risus lectus, sit amet pharetra lorem cursus rutrum.
Nunc ullamcorper purus massa, nec lobortis dui scelerisque id.
Proin eget suscipit odio.
Nulla sagittis urna eget viverra finibus.

Nullam in risus eu eros efficitur fringilla.
Proin porttitor consequat malesuada.
Quisque consectetur bibendum ligula, et molestie justo dapibus sed.
Pellentesque ultricies dignissim maximus.
Duis neque tellus, bibendum a felis ac, dignissim rhoncus turpis.
Mauris dictum a felis ut viverra. Sed eu mollis libero.
open('text.txt').readlines()

Would result in:

['Hello, World!\n',
 '\n',
 'Lorem ipsum dolor sit amet, consectetur adipiscing elit.\n',
 'Sed fermentum justo a dignissim lobortis.\n',
 'Nullam dapibus risus lectus, sit amet pharetra lorem cursus rutrum.\n',
 'Nunc ullamcorper purus massa, nec lobortis dui scelerisque id.\n',
 'Proin eget suscipit odio.\n',
 'Nulla sagittis urna eget viverra finibus.\n',
 '\n',
 'Nullam in risus eu eros efficitur fringilla.\n',
 'Proin porttitor consequat malesuada.\n',
 'Quisque consectetur bibendum ligula, et molestie justo dapibus sed.\n',
 'Pellentesque ultricies dignissim maximus.\n',
 'Duis neque tellus, bibendum a felis ac, dignissim rhoncus turpis.\n',
 'Mauris dictum a felis ut viverra. Sed eu mollis libero.\n']

A Better Way

Using the splitlines() command, we have a cleaner result:

open('text.txt').read().splitlines()

Results in no newline characters (\n) appended to the end of lines:

['Hello, World!',
 '',
 'Lorem ipsum dolor sit amet, consectetur adipiscing elit.',
 'Sed fermentum justo a dignissim lobortis.',
 'Nullam dapibus risus lectus, sit amet pharetra lorem cursus rutrum.',
 'Nunc ullamcorper purus massa, nec lobortis dui scelerisque id.',
 'Proin eget suscipit odio.',
 'Nulla sagittis urna eget viverra finibus.',
 '',
 'Nullam in risus eu eros efficitur fringilla.',
 'Proin porttitor consequat malesuada.',
 'Quisque consectetur bibendum ligula, et molestie justo dapibus sed.',
 'Pellentesque ultricies dignissim maximus.',
 'Duis neque tellus, bibendum a felis ac, dignissim rhoncus turpis.',
 'Mauris dictum a felis ut viverra. Sed eu mollis libero.']
Nicholas Nadeau, Ph.D., P.Eng.
Nicholas Nadeau, Ph.D., P.Eng.
Project Director

Nicholas Nadeau is the project director at Halodi Robotics, leading their mission of bringing safe and capable humanoid robots to everyone.

Related