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'\开发者_如何学CbDeposit', line):
print line.split(' ')
f.close()
Thanks
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']
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()
linesAsLists = [line.split() for line in open('myfile.txt', 'r') if 'Deposit' in line)]
Why not do line.strip()
before handling it? Also, you could use re.split
to use a regex like '\s+' as your delimiter.
精彩评论