[Tutor] Parsing txt file
Danny Yoo
dyoo at hashcollision.org
Thu Aug 21 00:48:35 CEST 2014
> i need to export to file all stings which start from "Name"
>
> I've tried to make little parser:
>
> keywords = ['Name', 'Name:']
> input_file=open("Mail_Groups.txt","r").readlines()
> output_file=open("Out.txt","w")
> for line in input_file:
> for word in line.split():
> if word in keywords:
> output_file.write(line)
>
> but the output is clear.
Questions:
1. How are you determining that the output is clear? I don't know
what you mean by "output is clear".
2. Does your program close() the file that it is writing to? See:
https://docs.python.org/2/library/stdtypes.html#file.write
https://docs.python.org/2/library/stdtypes.html#file.close
as to why this may be important.
3. Your problem description says:
i need to export to file all stings which start from "Name"
but the code here:
for word in line.split():
if word in keywords:
...
is doing much more than what is required. It is checking whether
"Name" shows up anywhere on the line, not just as part of the name of
the left-hand-side key. Is that ok?
More information about the Tutor
mailing list