-2

I am trying to delete blank lines from text file. Following is my take on it

aa=range(1,10)
aa[3]=""
print aa
[1, 2, 3, '', 5, 6, 7, 8, 9]

for i in range(0,len(aa)):
    if aa[i]=="":
        del aa[i]
print lines
[1, 2, 3, 5, 6, 7, 8, 9]

Now I am trying to replicate same methodology on the text file to delete a blank line but it is not working.

f=open("sample.txt",'r')
lines=[]
for i in f:
    lines.append(i)

print lines
['In this 30th match\n', '\n', 'there will be no1 winner']

for i in range(0,len(lines)):
    if lines[i]=="":
        del lines[i]

print lines
['In this 30th match\n', '\n', 'there will be no1 winner']
Abacus
  • 33
  • 3
  • if len(lines[i].rstrip()) < 1 : del lines[i] – dsgdfg Jul 18 '15 at 18:16
  • Check these links: http://stackoverflow.com/questions/2369440/how-to-delete-all-blank-lines-in-the-file-with-the-help-of-python, http://stackoverflow.com/questions/6745854/one-liner-for-removing-blank-lines-from-a-file-in-python , http://stackoverflow.com/questions/10794245/removing-spaces-and-empty-lines-from-a-file-using-python – Vinkal Jul 18 '15 at 18:28

2 Answers2

1

You can use if lines[i].isspace(): instead of if lines[i]=="":.

Example

>>> 'hello'.isspace()
False
>>> '\n'.isspace()
True

This should work fine:

your_file=open("file_name.ext")
without_blanks=[x for x in your_file if not x.isspace()]
your_file.close()

for line in without_blanks:
    print line
ForceBru
  • 43,482
  • 10
  • 63
  • 98
  • Thanks ForceBru, isspace() works well. Now there is another problem. Once it deletes one line the index of the list items changes and returns an index out of range error. for example: Deleting 2nd line. third line would take the index of second line and when the loop would reach to last iterations it would fall short of number of lines deleted – Abacus Jul 18 '15 at 18:26
  • You're iterating over your list in a way that has come from C or C++, I suppose. In Python one should iterate using `for something in iterable:`. Please see my edited answer. – ForceBru Jul 18 '15 at 18:31
  • Thanks ForceBru, isspace() works well. Now there is another problem. Once it deletes one line the index of the list items changes and returns an index out of range error. for example: lines = [1,' ', 3, 5, ' ', 23] Deleting 2nd item. third item would take the index of second item and when the loop would reach to its last iterations it would fall short of number of lines deleted. I am thinking of not deleting the blank lines but making a new list with only non-blank lines. any other suggestion for this. – Abacus Jul 18 '15 at 18:31
  • @Abacus, sure, I've already edited my answer, please check it. – ForceBru Jul 18 '15 at 18:33
  • Hey ForceBru, your method works pretty cool. Thanks a lot. Just on a personal note I like to iterate over the index of the list, it helps me visualize the loops more clearly. – Abacus Jul 18 '15 at 19:01
0

First, altering a sequence (deleting elements from it), while iterating is not a good idea.

Also a line that you see as bank is not actually blank, it might contain some SPACE, TAB (, or other) characters, but (as seen when printing your line variable) it will contain an EOLN marker: \n (or \r\n) on Windows.

As a result you could modify the code, after first printing lines, do this:

lines[:] = [item for item in lines if item.strip()]
CristiFati
  • 38,250
  • 9
  • 50
  • 87