D
dont bother
Hi Buddies,
I am facing this problem and I dont know what to use
as EOF in python:
I want to read a file, and put all the individual
words in a dictionary with their index:
For example if the file is:
Hello there I am doing fine
How are you?
So I want to make an index like this:
1 Hello
2 there
3 I
4 am
5 doing
6 fine
7 How
8 are
9 you
10 ?
In order to do this: I have written a small code which
is here:
-------------------------------------------------------
# python code for creating dictionary of words from an
#input file
------------------------------------------------------
import os
import sys
try:
fread = open('training_data', 'r')
except IOError:
print 'Cant open file for reading'
sys.exit(0)
print 'Okay reading the file'
s=""
a=fread.read(1)
while (a!="\003"):
#while 1:
s=s+a
if(a=='\012'): #newline
#print s
#print 'The Line Ends'
fwrite=open('dictionary', 'a')
fwrite.write(s)
s=""
if(a=='\040'): #blank character
#print s
fwrite=open('dictionary', 'a')
fwrite.write(s)
fwrite.write("\n")
s=""
a=fread.read(1)
print 'Wrote to Dictionary\n'
fwrite.close()
fread.close()
---------------------------------------------------
My problem is that I dont know what to use in place of
EOF. I have tried using Octal "\003" and "\004" but
that does not work. The code keeps on running. I want
it to stop reading when the EOF has reached.
Can someone help me out on this?
Also, I have to create a list: (A Map kind of thing
with an index associated with each word). Can some one
offer a tip or snippet on that.
I will be really grateful.
Thanks
Dont
__________________________________
Do you Yahoo!?
Yahoo! Mail SpamGuard - Read only the mail you want.
http://antispam.yahoo.com/tools
I am facing this problem and I dont know what to use
as EOF in python:
I want to read a file, and put all the individual
words in a dictionary with their index:
For example if the file is:
Hello there I am doing fine
How are you?
So I want to make an index like this:
1 Hello
2 there
3 I
4 am
5 doing
6 fine
7 How
8 are
9 you
10 ?
In order to do this: I have written a small code which
is here:
-------------------------------------------------------
# python code for creating dictionary of words from an
#input file
------------------------------------------------------
import os
import sys
try:
fread = open('training_data', 'r')
except IOError:
print 'Cant open file for reading'
sys.exit(0)
print 'Okay reading the file'
s=""
a=fread.read(1)
while (a!="\003"):
#while 1:
s=s+a
if(a=='\012'): #newline
#print s
#print 'The Line Ends'
fwrite=open('dictionary', 'a')
fwrite.write(s)
s=""
if(a=='\040'): #blank character
#print s
fwrite=open('dictionary', 'a')
fwrite.write(s)
fwrite.write("\n")
s=""
a=fread.read(1)
print 'Wrote to Dictionary\n'
fwrite.close()
fread.close()
---------------------------------------------------
My problem is that I dont know what to use in place of
EOF. I have tried using Octal "\003" and "\004" but
that does not work. The code keeps on running. I want
it to stop reading when the EOF has reached.
Can someone help me out on this?
Also, I have to create a list: (A Map kind of thing
with an index associated with each word). Can some one
offer a tip or snippet on that.
I will be really grateful.
Thanks
Dont
__________________________________
Do you Yahoo!?
Yahoo! Mail SpamGuard - Read only the mail you want.
http://antispam.yahoo.com/tools