Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Reading/Editing large text files

Status
Not open for further replies.

mickey17

Technical User
Oct 29, 2003
1
US
I am a newbie! yay! i would love it if someone can show me how to copy part of a text file to a new text file. *for instance assuming that the part of the file i want to copy goes from 'tommy' to 'jenny'
 
dunno if this makes sense, but it prints all the lines from the flat file 'CLIENTS.CSV' where the first letter is L

I am sure there are easy ways - but it's all I could think of on the spur of the moment.

I tried it on a 24mb client file and it only took a second to work out (took ages to display it to screen)

Peter


file = file('d:\\CLIENTS.CSV').readlines()
startline, endline, counter = 0, 0, 0
for line in file:
counter = counter +1
if line.startswith('L') and startline==0:
startline = counter
endline = counter
if line.startswith('M') and startline==endline:
endline = counter

print file[startline:endline]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top