Split single file into multiple files based on patterns

David Hutto dwightdhutto at gmail.com
Wed Oct 24 01:24:41 EDT 2012


On Tue, Oct 23, 2012 at 11:01 PM, satyam <dirac.sat at gmail.com> wrote:
> I have a text file like this
>
> A1980JE39300007 2732 4195 12.527000
> A1980JE39300007 3465 9720 22.000000
> A1980JE39300007 1853 3278 12.500000
> A1980JE39300007 2732 2732 187.500000
> A1980JE39300007 19 4688 3.619000
> A1980JE39300007 2995 9720 6.667000
> A1980JE39300007 1603 9720 30.000000
> A1980JE39300007 234 4195 42.416000
> A1980JE39300007 2732 9720 18.000000
> A1980KK18700010 130 303 4.985000
> A1980KK18700010 7 4915 0.435000
> A1980KK18700010 25 1620 1.722000
> A1980KK18700010 25 186 0.654000
> A1980KK18700010 50 130 3.199000
> A1980KK18700010 186 3366 4.780000
> A1980KK18700010 30 186 1.285000
> A1980KK18700010 30 185 4.395000
> A1980KK18700010 185 186 9.000000
> A1980KK18700010 25 30 3.493000
>
> I want to split the file and get multiple files like A1980JE39300007.txt and A1980KK18700010.txt, where each file will contain column2, 3 and 4.
> Thanks
> Satyam




#parse through the lines
turn_text_to_txt = ['A1980JE39300007 2732 4195 12.527000',
					'A1980JE39300007 3465 9720 22.000000',
					'A1980JE39300007 1853 3278 12.500000',
					'A1980JE39300007 2732 2732 187.500000',
					'A1980JE39300007 19 4688 3.619000',
					'A1980KK18700010 30 186 1.285000',
					'A1980KK18700010 30 185 4.395000',
					'A1980KK18700010 185 186 9.000000',
					'A1980KK18700010 25 30 3.493000']
#then split and open a file for writing to create the file

#then start a count to add an extra number, because the files #you're
opening have the same name in some, which will #cause python to
overwrite the last file with that name.

#So I added an extra integer count after an underscore to #keep all
files, even if the have the first base number.

count = 0

for file_data in turn_text_to_txt:

#open the file for writing in 'w' mode so it creates the file, and
#adds in the appropriate data, including the extra count i#nteger just
in case there are files with the same name.

	f = open('/home/david/files/%s_%s.txt' % (file_data.split(' ')[0], count), 'w')
	
#write the data to the file, however this is in list format, I could
go further, but need a little time for a few other things.

	f.write( str(file_data.split(' ')[1:]))

#close the file	
	f.close()

#increment the count for the next iteration, if necessary, and #again,
this is just in case the files have the same name, and #need an
additive.
#	count += 1


Full code from above, without comments:

turn_text_to_txt = ['A1980JE39300007 2732 4195 12.527000',
					'A1980JE39300007 3465 9720 22.000000',
					'A1980JE39300007 1853 3278 12.500000',
					'A1980JE39300007 2732 2732 187.500000',
					'A1980JE39300007 19 4688 3.619000',
					'A1980KK18700010 30 186 1.285000',
					'A1980KK18700010 30 185 4.395000',
					'A1980KK18700010 185 186 9.000000',
					'A1980KK18700010 25 30 3.493000']
#then split and open a file for writing to create the file
count = 0

for file_data in turn_text_to_txt:

	print '/home/david/files/%s.txt' % (file_data.split(' ')[0])

	f = open('/home/david/files/%s_%s.txt' % (file_data.split(' ')[0], count), 'w')
	
	f.write( str(file_data.split(' ')[1:]))
	
	f.close()
	
	count += 1




-- 
Best Regards,
David Hutto
CEO: http://www.hitwebdevelopment.com



More information about the Python-list mailing list