eozzy
eozzy

Reputation: 68710

Removing empty items from a list (Python)

I'm reading a file in Python that isn't well formatted, values are separated by multiple spaces and some tabs too so the lists returned has a lot of empty items, how do I remove/avoid those?

This is my current code:

import re

f = open('myfile.txt','r') 

for line in f.readlines(): 
    if re.search(r'\bDeposit', line):
        print line.split(' ')

f.close()

Thanks

Upvotes: 2

Views: 5780

Answers (4)

ghostdog74
ghostdog74

Reputation: 342649

for line in open("file"):
    if " Deposit" in line:
         line=line.rstrip()
         print line.split()

Update:

for line in open("file"):
    if "Deposit" in line:
         line=line.rstrip()
         print line[line.index("Deposit"):].split()

Upvotes: 2

Caleb Hattingh
Caleb Hattingh

Reputation: 9225

linesAsLists = [line.split() for line in open('myfile.txt', 'r') if 'Deposit' in line)]

Upvotes: 1

Max Shawabkeh
Max Shawabkeh

Reputation: 38643

Don't explicitly specify ' ' as the delimiter. line.split() will split on all whitespace. It's equivalent to using re.split:

>>> line = '  a b   c \n\tg  '
>>> line.split()
['a', 'b', 'c', 'g']
>>> import re
>>> re.split('\s+', line)
['', 'a', 'b', 'c', 'g', '']
>>> re.split('\s+', line.strip())
['a', 'b', 'c', 'g']

Upvotes: 11

dubiousjim
dubiousjim

Reputation: 4822

Why not do line.strip() before handling it? Also, you could use re.split to use a regex like '\s+' as your delimiter.

Upvotes: 0

Related Questions