8

I'm reading lines from a file that contains one[*] word/line, such as:

dog
cat
person
tree

Each of these words also contains a newline \n character. I want to read them into a list and throw away the newlines. The way I've devised is to read with readlines() and then process the list to strip() the newlines:

with open('words.txt') as f:
    words = f.readlines()

for index, word in enumerate(words):
    words[index] = word.strip()

This works fine, but I can't help thinking there's a more efficient way to do this, to strip the newlines during the read process. But I can't find a way. Is there something more efficient (while also considering readability, etc.)

[*] UPDATE: I should have mentioned that some lines may contain more than one word, and in those cases however many words are on a line should go into a single list item. Both answers so far handle this (as does my own code), but I wanted to mention it.

mix
  • 6,509
  • 14
  • 57
  • 88
  • You can do it like this: # ble is one column file: lista = [] with open("ble", "r") as f: for i in f: i = i.strip() lista.append(i) – Irka Irenka Oct 12 '21 at 18:15

4 Answers4

15

You could use a list comprehension:

with open('words.txt') as f:
    words = [word.strip() for word in f]
Tim Pietzcker
  • 313,408
  • 56
  • 485
  • 544
5

You can use map:

with open('words.txt') as f:
   words = map(str.rstrip, f)
Ashwini Chaudhary
  • 232,417
  • 55
  • 437
  • 487
  • 3
    rstrip, if you want to be even more precise :) (not that it's needed or anything) – TerryA Sep 18 '13 at 06:24
  • And even `rstrip("\n")` to remove only the newline character (or `rstrip("\n\r")` to remove both newline and carriage return). –  Sep 18 '13 at 06:31
2

You could write: lines = [s.rstrip("\n\r") for s in f.readlines()] (notice it's not just strip, which will do more than remove EOL characters).

However, if your file is large, you should maybe process each line in a loop, rather than laoding the whole file, for example as in:

while True:
    s = f.readline()
    if s == "":
        break   # end of file
    line = s.rstrip("\n\r")
    ...
2

For handling more than one word per line you may want to split the line.

with open('words.txt') as f:
    result = [words.strip().split() for words in f]

This will create a list of lists, most of which are one element long. So, for example you could do this.

for words in result:
    print len(words)
Graeme Stuart
  • 5,277
  • 1
  • 23
  • 43