Skip to content

Instantly share code, notes, and snippets.

@sposterkil
Created November 12, 2013 17:59
Show Gist options
  • Save sposterkil/7435569 to your computer and use it in GitHub Desktop.
Save sposterkil/7435569 to your computer and use it in GitHub Desktop.
EECS293 CSV translation project
#!/usr/bin/env python
import fileinput
import re
import sys
def _prepare_line_list(line):
line_list = re.split("\s+", line)
# Use of re.split adds an empty element to the list
del line_list[len(line_list) - 1]
_check_field_quantity(line_list)
_check_field_values(line_list)
del line_list[5]
return line_list
def _check_field_quantity(line_list):
# Check that we have the proper number of fields
if len(line_list) not in [6, 7]:
raise ValueError("Incorrect number of fields on line %d. Bad file?" %
fileinput.filelineno())
# Add empty field if we don't have a middle name
if len(line_list) is 6:
line_list.insert(3, "")
def _check_field_values(line_list):
if not line_list[0].isdigit():
raise ValueError(
"First field on line %d is not all digits. Bad file?" %
fileinput.filelineno())
if not line_list[5] == "F13":
raise ValueError('Fifth Field on line %d is not "F13". Bad file?' %
fileinput.filelineno())
if not line_list[6].startswith("S"):
raise ValueError(
'Sixth field on line %d does not start with "S". Bad file?' %
fileinput.filelineno())
def _capitalize_at_index(string, index):
if index < len(string):
return string[:index] + string[index].upper() + string[index + 1:]
else:
return string
def _capitalize(line_list):
# This is an indexed loop instead of list iteration because we only
# want to operate on indices [1,4], but slicing creates a new list.
for i in xrange(1, 4):
line_list[i] = line_list[i].title()
∏ if (line_list[i].startswith("Mc", 0, 2) or
line_list[i].startswith("O'", 0, 2)):
line_list[i] = _capitalize_at_index(line_list[i], 2)
elif line_list[i].startswith("Mac", 0, 2):
line_list[i] = _capitalize_at_index(line_list[i], 4)
return line_list
def main():
try:
for line in fileinput.input():
line_list = _prepare_line_list(line)
_capitalize(line_list)
print ",".join(line_list)
finally:
fileinput.close()
if __name__ == '__main__':
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment