473,386 Members | 1,721 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,386 software developers and data experts.

read in from file

Hi all,

trying to write a program that will read a file in like:

python myprog < inputFile

the program needs to store each line a an entry in a list.

Expand|Select|Wrap|Line Numbers
  1.  
  2. li = []
  3.  
  4.  
  5. for i in range(0,9): #<- these must be the correct range.    
  6.     temp = raw_input("")
  7.     li.append(temp)
  8.  
  9.  

This code works but you have to hard code how many lines there will be (see comment). I need my program to terminate at the EOF. I have looked around for a couple hours but havent had enormous luck finding anything.

Tryed entering in

Expand|Select|Wrap|Line Numbers
  1.  
  2. if(temp==\0):
  3.       break
  4.  
  5.  
and similar but not having to much luck. Thanks for reading!

JS
Oct 5 '08 #1
3 1300
bvdet
2,851 Expert Mod 2GB
Iteration on a file object will terminate when the EOF character is reached.
Expand|Select|Wrap|Line Numbers
  1. f = open('file.txt')
  2. for i, line in enumerate(f):
  3.     print 'Line number %d' % i
  4.     ## do more stuff
  5.  
  6. f.close()
  7.  
Oct 5 '08 #2
Thanks bvdet .

For anyone with a similar problem as me this is the way I ended up doing it:

Expand|Select|Wrap|Line Numbers
  1.  
  2. def readWords():
  3.     while 1:
  4.         line = raw_input()
  5.         if line != "":
  6.             listOfWords.append(line);
  7.         else:
  8.             break
  9. #End read words method
  10.  
  11.  
Oct 5 '08 #3
Ooops. The previous code does not detect EOF (well it does sometimes, just not not always...)

Regardless, this code works:

Expand|Select|Wrap|Line Numbers
  1.  
  2. # reads words from the input file
  3. def readWords():
  4.     jake = sys.stdin
  5.     listOfWords= jake.readlines()
  6.     listOfWords = [i[:-1] for i in listOfWords]
  7.     return listOfWords
  8.  
  9.  
  10.  
Oct 7 '08 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

10
by: Yang Li Ke | last post by:
Hi guys! I have some datas that I must check everytime a visitor comes to my site What is better to do: 1- Read data from a file or 2- Read data from a mysql db Thank you
2
by: Gunnar | last post by:
Hello, I've just written a CPP program that reads integers from a binary file, and used this code while (my_ifstram.read( (char* ) &number, sizeof(int)) { // do something with number } My...
5
by: Pete | last post by:
I having a problem reading all characters from a file. What I'm trying to do is open a file with "for now" a 32bit hex value 0x8FB4902F which I want to && with a mask 0xFF000000 then >> right...
4
by: ESPN Lover | last post by:
Below is two snippets of code from MSDN showing how to read a file. Is one way preferred over the other and why? Thanks. using System; using System.IO; class Test { public static void...
3
by: nicolasg | last post by:
Hi, I'm trying to open a file (any file) in binary mode and save it inside a new text file. After that I want to read the source from the text file and save it back to the disk with its...
6
by: =?Utf-8?B?VGhvbWFzWg==?= | last post by:
Hi, Is it possible to read a file in reverse and only get the last 100 bytes in the file without reading the whole file from the begining? I have to get info from files that are in the last 100...
9
by: flebber | last post by:
I was working at creating a simple program that would read the content of a playlist file( in this case *.k3b") and write it out . the compressed "*.k3b" file has two file and the one I was trying...
2
by: Kevin Ar18 | last post by:
I posted this on the forum, but nobody seems to know the solution: http://python-forum.org/py/viewtopic.php?t=5230 I have a zip file that is several GB in size, and one of the files inside of it...
9
by: vineeth | last post by:
Hello all, I have come across a weird problem, I need to determine the amount of bytes read from a file, but couldn't figure it out , My program does this : __ file = open("somefile") data =...
2
by: =?Utf-8?B?c2lwcHl1Y29ubg==?= | last post by:
Hi Is there any way to Read an INIFile from a string or Stream instead of a physical file ??? I want to read the INIFile into a string then store in a db but when I read the string from the...
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
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
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
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.