Last active
August 29, 2015 13:56
-
-
Save sangheestyle/9088708 to your computer and use it in GitHub Desktop.
Parse a file to make group items.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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") |
Output:
$ python parse_doc.py
{'MEMBER': [['tom', 'sanghee', 'mike'], ['a', 'b', 'c'], ['a', 'a', 'a']], 'INFO': [['TThis is my love for you. This is my love for you. This is my love for you. This is my love for you. This is my love for you. This is my love for you. his is my love for you. This is my love for youb'], ['a'], ['b', 'b.']]}
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
input: team.txt
MEMBER
tom
sanghee
mike
INFO
TThis is my love for you. This is my love for you. This is my love for you. This is my love for you. This is my love for you. This is my love for you. his is my love for you. This is my love for youb
MEMBER
a
b
c
INFO
a
MEMBER
a
a
a
INFO
b
b.