python - Whitespace removing in file -


i want remove whitespaces in file using python. that, using:

output = "file.out"  open(output) templist:     templ = templist.read().splitlines()  filelist = [] line in templ:     if line.startswith("something"):         filelist.append(line.strip(' ')) 

i managed append required lines, whitespace removing not work. can me? =]

.strip() removes whitespace start , end of string.

i think easiest way go use regex:

import re ... line in templ:     if line.startswith("something"):         filelist.append(re.sub(r"\s+", "", line)) 

\s matches kind of whitespace (spaces, newlines, tabs etc.).


Comments

Popular posts from this blog

Android layout hidden on keyboard show -

google app engine - 403 Forbidden POST - Flask WTForms -

c - Why would PK11_GenerateRandom() return an error -8023? -