Put the regular expression hammer away :-)
- You can iterate over a file directly;
readlines()
is almost obsolete these days.
- Read about
str.strip()
(and its friends, lstrip()
and rstrip()
).
- Don't use
file
as a variable name. It's bad form, because file
is a built-in function.
You can write your code as:
lines = []
f = open(filename)
for line in f:
if not line.startswith('com'):
lines.append(line.strip())
If you are still getting blank lines in there, you can add in a test:
lines = []
f = open(filename)
for line in f:
if line.strip() and not line.startswith('com'):
lines.append(line.strip())
If you really want it in one line:
lines = [line.strip() for line in open(filename) if line.strip() and not line.startswith('com')]
Finally, if you're on python 2.6, look at the with statement to improve things a little more.