Skip to content

Instantly share code, notes, and snippets.

@xlevus
Created October 16, 2012 15:22
Show Gist options
  • Save xlevus/3899936 to your computer and use it in GitHub Desktop.
Save xlevus/3899936 to your computer and use it in GitHub Desktop.
Guess what this does
def func(lines):
first_pass = list()
#now clean any preceding whitespace
for index, line in enumerate(lines):
table = string.maketrans(string.whitespace, ' ' * len(string.whitespace))
first_pass.append(string.translate(line, table))
clean = list()
for line in first_pass:
c = line.replace('\n', '')
if c != '' and len(c) > 1:
count = 0
for char in c: #if the line isn't totally blank, add it.
if char != ' ':
count += 1
if count > 1:
clean.append(c)
break
for index, line in enumerate(clean):
white_chars = 0
for char in line:
if char == ' ':
white_chars += 1
else:
line = line[white_chars:]
break
i = len(line)
count = 0
while i > 0: #clean trailing whitespace.
if line[i - 1] == ' ':
count -= 1
else:
if count != 0:
line = line[:count]
break
i -= 1
#last but not least, remove any ';' characters from the end
if line[len(line) - 1] == ';':
line = line[:-1]
clean[index] = line
#this is to fix windows dropping bytes on the front of the file.
if clean[0].startswith(codecs.BOM_UTF8):
clean[0] = clean[0].replace(codecs.BOM_UTF8, '')
return clean
@somabc
Copy link

somabc commented Oct 16, 2012

A+

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment