[Tutor] parsing .txt
glist
gmlists9 at gmail.com
Tue Jun 10 12:40:39 CEST 2008
Let's say I have a file with this kind of content/lines
textdata = '''
%question What is Python?
%correct A programming language
%wrong A graphical package
%wrong An operating system
%question Is Computer Science really about computers?
%wrong Yes
%correct No
'''
I want to pose the question to a user and check his answer, so I come up
with this solution. Is there a better
way thought? I particulary hate that "ptr = -1" hack of mine...
questionary = []
ptr = -1
for line in textdata.split('\n'):
if line:
space = line.find(' ')
tag = line[1:space]
content = line[space+1:]
if tag == "question":
ptr += 1
questionary.append({tag : content})
else:
questionary[ptr].update({tag : content})
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20080610/fa0b6727/attachment.htm>
More information about the Tutor
mailing list