[Tutor] raw_input()

Steven D'Aprano steve at pearwood.info
Tue Mar 16 00:25:34 CET 2010


On Tue, 16 Mar 2010 09:52:26 am kumar s wrote:
> Dear group:
> I have a large file 3GB. Each line is a tab delim file.
[...]
> Now I dont want to load this entire file. I want to give each line as
> an input and print selective lines.

datafile = open("somefile.data", "r")
for line in datafile:
    print line

will print each line. If you want to print selected lines, you have to 
explain what the condition is that decides whether to print it or not. 
I'm going to make something up: suppose you want the line to only print 
if the eighth column is "A":

def condition(line):
    line = line.strip()
    fields = line.split('\t')
    return fields[7].strip().upper() == "A"


datafile = open("somefile.data", "r")
for line in datafile:
    if condition(line):
        print line

will print only the lines where column 8 is the letter A.



-- 
Steven D'Aprano


More information about the Tutor mailing list