[Tutor] Another ? on large text files

Hugo González Monteverde hugonz at h-lab.net
Wed Dec 8 23:26:06 CET 2004


Hi Ara,

I'd go something like this:

filep = open("myfile")
in_data = False
output_files = ["file1.txt", "file2.txt" ..... ]


for i in output_files:
	while True:
		dataline = filep.readline()
		if not dataline:
			break

		if not in_data:
			if dataline.startswith("CERIUS Grapher File"):
			ofilep = open(i,"w")
			#say there are 5 more lines in header
			for i in range(6): filep.readline()
			in_data=True
			continue
		else:
			if dataline.startswith(
			"PLOT XY METHOD:\"Power"):
				ofilep.close()
				break
			else:
				ofilep.write(filep.readline())
			

			
		

I basically detect start of header and start of footer, and intepret 
thus if I'm inside a bunch of data. This is very fragile to malformed 
input and will likely block or lose data if the file is not strictly 
set. But it should work...

Hugo

	


Kooser, Ara S wrote:
> I have a large file (4.1 MB, 2600 pgs) of simulated power spectrums. The
> problem is that there are 6 separated files all combined into this large
> file. I know how to read the file in and then remove the header
> information but how would I go about separating the 6 sections into
> separate files?
> 
> The program I have so far reads in the data and then removes the header
> and footer information (most likely in a very inefficient fashion).
> 
> The data looks something like this and I just want the numbers.
> 
> 
> CERIUS Grapher File
> ! Start of definition for XY graph Power Spectrum component XX A^4/ps^2
> v1
> 
>>PLOT XY DATA: "Power Spectrum component XX A^4/ps^2" 1
> 
>  0.0000000E+00   3.1251088E-04
> ..<a bunch of stuff)............................
>  3333.167       2.2011892E-07
> 
>>PLOT XY METHOD: "Power Spectrum component YY A^4/ps^2" 1
> 
>  COLOUR  RED 
> .......
> The footer continues and then runs into the header of the next set of
> data (no space between them)
> 
> Thanks,
> Ara
> 
> 
> 
> 
> _______________________________________________
> Tutor maillist  -  Tutor at python.org
> http://mail.python.org/mailman/listinfo/tutor
> 


More information about the Tutor mailing list