473,387 Members | 1,641 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Help With EOF character: URGENT

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

Jul 18 '05 #1
2 21623
On Sunday 22 February 2004 20:10, dont bother wrote:
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?


Have you tried just using "while a:"? When it can't read anything -- i.e.
EOF was found -- it will return False and the loop will end.
Be seeing you,
--
Godoy. <go***@ieee.org>
Jul 18 '05 #2
dont bother wrote:
Hi Buddies,
I am facing this problem and I dont know what to use
as EOF in python:


There's no "EOF character" in Python. When the end of a file is
reached, reading from it returns an empty string. To process
a file one character at a time, you can do

while 1:
c = f.read(1)
if not c:
break
# process c here

In your case you seem to be dealing with words, so you can
take advantage of two Python features: (1) You can read
a line at a time with the readline() method. (2) You can
split a string into words with the split() method of strings.

while 1:
line = f.readline()
if not line:
break
words = line.split()
for word in words:
# process word here

If you have a recent enough Python (>= 2.2 I think), you can
also iterate directly over the file, which will iterate over
its lines, so the above reduces to just

for line in f:
words = line.split()
for word in words:
# process word here

Note: The readline() method, and also "for line in f", returns
lines including the newline character on the end. That doesn't
matter here, because line.split() gets rid of all the whitespace,
but you need to be aware of it if you do other things with
the line. You can use

line = line.strip()

to remove the newline if you need to.

--
Greg Ewing, Computer Science Dept,
University of Canterbury,
Christchurch, New Zealand
http://www.cosc.canterbury.ac.nz/~greg

Jul 18 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Edwinah63 | last post by:
Hi Everyone, All the very best for 2004!! i need urgent help with this problem, the users are about to skin me alive!! we have an access front end with linked to sql server 2k tables. ...
4
by: Carl Harris | last post by:
I am trying to write some code to: 1.Prompt a user for filenames 2.Open the files 3.Convert my plain text into a cipher text array/string bear in mind I am a novice! I have wriiten some code...
4
by: DraguVaso | last post by:
Hi, I have files I need to read, which contains records with a variable lenght. What I need to do is Copy a Part of such a File to a new File, based on the a Begin- and End-record. I used...
0
ekekakos
by: ekekakos | last post by:
Hello, I am having a serious and urgent problem with the character settings of an oracle database. The database is sitting in a solaris unix server and when we run the env command we have the...
2
by: Preetam Pattanashetty | last post by:
Hi I am learning ASP.NET using C#. I am able to run .aspx files on local system, but when I load them to the server, I get the "Server Error in '/' Application" error. I have tried to configure the...
2
by: vnsmanian2006 | last post by:
Hello Friends, 1) I need the syntax for Control arrays. Actually i use control arrays in vb but i could'nt find PHP syntax for control array even in google search. if u give me...
5
by: ianenis.tiryaki | last post by:
i have thiscode and i have to figure out what character would be displayed at line 23 but they are using this f1 functin so i am confused.... please help me which character would be displayed? ...
2
by: geniuskanwal | last post by:
Before I begin to explain my problem, I just want to say that I can do the following two things: 1. Using Perl, connect to a MS Access Databse Table and perform the required operations.(Database...
1
by: mohit1286 | last post by:
my stored procedure in db2 is---> create procedure temp_bill(in UPC_cd character(6)) language sql begin declare prod_cd character(8);declare prod_desc varchar(30);declare discount...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.