Skip to content

Instantly share code, notes, and snippets.

@sangheestyle
Last active August 29, 2015 13:56
Show Gist options
  • Save sangheestyle/9088708 to your computer and use it in GitHub Desktop.
Save sangheestyle/9088708 to your computer and use it in GitHub Desktop.
Parse a file to make group items.
from collections import defaultdict
def read_team_info(path):
status = "None"
total = defaultdict(list)
team_info = []
fp = open(path, "r")
for line in fp:
line = line.rstrip()
if len(line) == 0:
total[status].append(team_info)
team_info = []
status = ""
elif line.startswith("MEMBER") or status == "MEMBER":
if status != "MEMBER": status = "MEMBER"
else: team_info.append(line)
elif line.startswith("INFO") or status == "INFO":
if status != "INFO": status = "INFO"
else: team_info.append(line)
else:
raise NameError('Please check markup')
total[status].append(team_info)
fp.close()
return total
if __name__ == "__main__":
print read_team_info("team.txt")
@sangheestyle
Copy link
Author

Revisions 3:

Using high performance container datatypes for convenience.
http://docs.python.org/2/library/collections.html

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