regex - Spliting a file into lines in Python using re.split -
I am trying to split a file using the code, using the code:
lines = [split for x ("\ n +", x in file.read ()) if not re.match (r "com", x)]< / Pre>However, there is always the last element in the row list as the last string, does anyone know one way to avoid this (except for the scream of putting a pop () later)?
Keep regular expression hammer away: -)
- Can repeat with a file; << Li> and
rstrip ()). - Do not use
fileas a variable namefile.
You can type your code as:
line = [] f = if you are getting blank lines in the line: line (File com): open for 'line' (filename): lines .append (line.strip ()) , you can add it to a test:
line = [] f = open for line (filename) f: if line.strip () and not line.startswith ('com'): lines .append (line.strip ()) If you really want it in a row:
line = [line.strip () For the end of the line, if you are on dragon 2.6, then look for things to improve. slightly higher.
Comments
Post a Comment