views:

39

answers:

4

I have a very large (~8 gb) text file that has very long lines. I would like to pull out lines in selected ranges of this file and put them in another text file. In fact my question is very similar to this and this but I keep getting stuck when I try to select a range of lines instead of a single line.

So far this is the only approach I have gotten to work:

lines = readin.readlines()
out1.write(str(lines[5:67]))
out2.write(str(lines[89:111]))

However this gives me a list and I would like to output a file with a format identical to the input file (one line per row)

A: 

(Partial Answer) In order to make your current approach work you'll have to write line by line. For instance:

lines = readin.readlines()

for each in lines[5:67]:
    out1.write(each)

for each in lines[89:111]:
    out2.write(each)
Manoj Govindan
+1  A: 

You can call join on the ranges.

lines = readin.readlines()
out1.write(''.join(lines[5:67]))
out2.write(''.join(lines[89:111]))
Bob
It should be out1.write(''.join(lines[5:67])) and same for out2 because readlines doesn't remove end of lines
Xavier Combelle
@Xavier oops, fixed
Bob
+1  A: 

might i suggest not storing the entire file (since it is large) as per one of your links?

f = open('file')
n = open('newfile', 'w')
for i, text in enumerate(f):
    if i > 4 and i < 68:
        n.write(text)
    elif i > 88 and i < 112:
        n.write(text)
    else:
        pass

i'd also recommend using 'with' instead of opening and closing the file, but i unfortunately am not allowed to upgrade to a new enough version of python for that here : (.

asia1281
A: 
path = "c:\\someplace\\"

Open 2 text files. One for reading and one for writing

f_in = open(path + "temp.txt", 'r')
f_out = open(path + output_name, 'w')

go through each line of the input file

for line in f_in:
    if i_want_to_write_this_line == True:
        f_out.write(line)

close the files when done

f_in.close()
f_out.close()
Mike K