1

I have a file: Alus.txt

File content: (each name in new line)

Margus

Mihkel

Daniel

Mark

Juri

Victor

Marek

Nikolai

Pavel

Kalle

Problem: While programm reads this file, there are \n after each name (['Margus\n', 'Mihkel\n', 'Daniel\n', 'Mark\n', 'Juri\n', 'Victor\n', 'Marek\n', 'Nikolai\n', 'Pavel\n', 'Kalle']). How can I remove \n and have a list with names? What I am doing wrong? Thank you.

alus = []

file = open('alus.txt', 'r')

while True:

    rida = file.readline()

    if (rida == ''):

        break

    else:

        alus.append(rida)
SilentGhost
  • 307,395
  • 66
  • 306
  • 293
Bob
  • 10,427
  • 24
  • 63
  • 71

4 Answers4

4

You can remove the linebreaks with rstrip:

alus = []
with open('alus.txt', 'r') as f:
    for rida in f:
        rida=rida.rstrip()
        if rida: alus.append(rida)
        else: break

By the way, the usual way to test if a string is empty is

if not rida:

rather than

if (rida == ''):

And if you have an if...else block, you should consider the non-negated form:

if rida:

since it is usually easier to read and understand.

Edit: My previous comment about removing break was wrong. (I was mistaking break with continue.) Since break stops the loop, it needs to be kept to preserve the behavior of your original code.

Edit 2: A.L. Flanagan rightly points out that rstrip removes all trailing whitespace, not just the ending newline character(s). If you'd like to remove the newline characters only, you could use A.L. Flanagan's method, or list the characters you wish to remove as an argument to rstrip:

rida = rida.rstrip(r'\r\n')
Community
  • 1
  • 1
unutbu
  • 842,883
  • 184
  • 1,785
  • 1,677
4
alnus = [l.rstrip() for l in open('alus.txt', 'r')]
satoru
  • 31,822
  • 31
  • 91
  • 141
3

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

twneale
  • 2,836
  • 4
  • 29
  • 34
  • While the response marked as the answer is correct and informative, this is the more pythonic way. – Binary Phile Oct 31 '10 at 16:58
  • @max It makes no sense to preemptively introduce java-like complexity just because an unusual case might arise later. If it does, this is python: you delete one line and code a replacement. Simple is better. – twneale Apr 19 '12 at 18:06
  • @twneale I deleted my earlier comment because I provided the wrong URL. My comment was that your approach will load the whole file in memory; it also doesn't allow any processing of the lines. I recommend [this approach](http://stackoverflow.com/a/339842/336527). To answer your comment I agree with you that extra complexity isn't worth it unless you need it. If I take the OP question literally, he puts all the names into a list anyway, so your solution will work perfectly fine. – max Apr 20 '12 at 04:44
1

One possible problem with rstrip() is that it will remove any whitespace. If you want to preserve whitespace, you can use slices:

if line.endswith('\n'):
    line = line[:-1]

If you could be sure all the lines end with '\n', you could speed it up by removing the if. However, in general, you can't be sure the last line in a text file has a newline.

A. L. Flanagan
  • 1,162
  • 8
  • 22
  • "Slices" is nto a good solution here - if you have to care about gettign rid only of "\n", pass "\n" as a parameter do rstrip. – jsbueno Oct 31 '10 at 12:22