Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #31996

Re: Split single file into multiple files based on patterns

From Dennis Lee Bieber <wlfraed@ix.netcom.com>
Subject Re: Split single file into multiple files based on patterns
Date 2012-10-24 01:38 -0400
Organization > Bestiaria Support Staff <
References <9ae3b43a-5c69-4232-a1cf-e2dd0c4ee2ca@googlegroups.com> <CANy1k1jiHafh-mmhau158dAa3b6wPcfxv08igiT1W=hhAeBZWA@mail.gmail.com>
Newsgroups comp.lang.python
Message-ID <mailman.2720.1351057129.27098.python-list@python.org> (permalink)

Show all headers | View raw


On Tue, 23 Oct 2012 21:43:21 -0600, Jason Friedman <jason@powerpull.net>
declaimed the following in gmane.comp.python.general:

> $ python3
> Python 3.2.3 (default, Sep 10 2012, 18:14:40)
> [GCC 4.6.3] on linux2
> Type "help", "copyright", "credits" or "license" for more information.
> >>> for line in open("source"):
> ...     file_name, remainder = line.strip().split(None, 1)
> ...     with open(file_name + ".txt", "a") as writer:
> ...         print(remainder, file=writer)

	That's a lot of OS file open/closing operations...

	I'd be more likely to configure the code as a "standard" "report
control break".

control = None
fin = open("source")
for line in fin:
	newControl, data = line.split(None, 1)	#leave new-line for output
	if control != newControl:	#only open/close files on
								#change of control break
		if control:
			fout.close()
		fout = open(newControl + ".txt", "a")
			#I'd prefer using "w" IF the input is already sorted
			#that way one knows a new file is created on each run
			#instead of having to delete any existing files from 
			#previous runs
		control = newControl
	fout.write(data)
if control:
	fout.close()
fin.close()

-- 
	Wulfraed                 Dennis Lee Bieber         AF6VN
        wlfraed@ix.netcom.com    HTTP://wlfraed.home.netcom.com/

Back to comp.lang.python | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

Split single file into multiple files based on patterns satyam <dirac.sat@gmail.com> - 2012-10-23 20:01 -0700
  Re: Split single file into multiple files based on patterns Jason Friedman <jason@powerpull.net> - 2012-10-23 21:36 -0600
  Re: Split single file into multiple files based on patterns Jason Friedman <jason@powerpull.net> - 2012-10-23 21:43 -0600
  Re: Split single file into multiple files based on patterns David Hutto <dwightdhutto@gmail.com> - 2012-10-24 01:24 -0400
  Re: Split single file into multiple files based on patterns Demian Brecht <demianbrecht@gmail.com> - 2012-10-23 22:36 -0700
  Re: Split single file into multiple files based on patterns Dennis Lee Bieber <wlfraed@ix.netcom.com> - 2012-10-24 01:38 -0400
  Re: Split single file into multiple files based on patterns Alain Ketterlin <alain@dpt-info.u-strasbg.fr> - 2012-10-24 07:46 +0200
    Re: Split single file into multiple files based on patterns Mark Lawrence <breamoreboy@yahoo.co.uk> - 2012-10-24 07:13 +0100
  Re: Split single file into multiple files based on patterns Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2012-10-24 07:52 +0000
    Re: Split single file into multiple files based on patterns David Hutto <dwightdhutto@gmail.com> - 2012-10-24 04:02 -0400
  Re: Split single file into multiple files based on patterns Peter Otten <__peter__@web.de> - 2012-10-24 10:17 +0200

csiph-web