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)?

+1


source to share


4 answers


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()

    and rstrip()

    ).
  • Do not use file

    as a variable name. This is bad form because it file

    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.

+9


source


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 ())]

+3


source


Another handy trick, especially if you need a line number, is to use an enum:


fp = open("myfile.txt", "r")
for n, line in enumerate(fp.readlines()):
    dosomethingwith(n, line)

      

I only found out about enumeration quite recently, but it has come in handy quite a few times since then.

+1


source


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]

      

0


source







All Articles