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

  1. Can repeat with a file; << Li> and rstrip () ).
  2. Do not use file as a variable name file .

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

Popular posts from this blog

python - Overriding the save method in Django ModelForm -

html - CSS autoheight, but fit content to height of div -

qt - How to prevent QAudioInput from automatically boosting the master volume to 100%? -