Splitting a file into lines in Python using re.split
I am trying to split a combo file using code similar to:
lines = [x for x in re.split(r"\n+", file.read()) if not re.match(r"com", x)]
However, the string list always contains an empty string as the last item. Does anyone know how to avoid this (eliminating the possibility of placing pop () afterwards)?
source to share
Place the correct expression with a hammer :-)
- You can iterate over the file directly;
readlines()
almost outdated these days. - Read about
str.strip()
(and his friends,lstrip()
andrstrip()
). - Do not use
file
as a variable name. This is bad form because itfile
is a built-in function .
You can write your code like:
lines = []
f = open(filename)
for line in f:
if not line.startswith('com'):
lines.append(line.strip())
If you still get blank lines, you can add to your 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 on one line:
lines = [line.strip() for line in open(filename) if line.strip() and not line.startswith('com')]
Finally, if you are on python 2.6, take a look with instructions to improve things a little more.
source to share
lines = file.readlines ()
change or if you don't need blank lines you can do
lines = filter (lambda a: (a! = '\ n'), file.readlines ())
edit ^ 2: to remove trailing new items you can do
lines = [re.sub ('\ n', '', line) for line in filter (lambda a: (a! = '\ n'), file.readlines ())]
source to share
This should work and eliminate regexes:
all_lines = (line.rstrip()
for line in open(filename)
if "com" not in line)
# filter out the empty lines
lines = filter(lambda x : x, all_lines)
Since you are using a list comprehension and not a generator expression (so all files are loaded into memory anyway), here's a shortcut that avoids code to filter out empty lines:
lines = [line
for line in open(filename).read().splitlines()
if "com" not in line]
source to share