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

Path csiph.com!usenet.pasdenom.info!weretis.net!feeder1.news.weretis.net!feeder.erje.net!newsfeed.xs4all.nl!newsfeed6.news.xs4all.nl!xs4all!post.news.xs4all.nl!not-for-mail
Return-Path <python-python-list@m.gmane.org>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.000
X-Spam-Evidence '*H*': 1.00; '*S*': 0.00; 'configure': 0.04; 'output': 0.04; 'python3': 0.05; 'subject:file': 0.07; 'python': 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'sep': 0.09; 'subject:files': 0.09; 'subject:into': 0.09; '"a")': 0.16; '"report': 0.16; 'oct': 0.16; 'received:80.91.229.3': 0.16; 'received:plane.gmane.org': 0.16; 'subject:based': 0.16; '>>>': 0.18; 'input': 0.18; 'runs': 0.22; "i'd": 0.22; 'header:X -Complaints-To:1': 0.28; 'run': 0.28; 'knows': 0.30; 'code': 0.31; 'file': 0.32; 'url:home': 0.33; 'to:addr:python-list': 0.33; 'likely': 0.33; 'jason': 0.35; 'received:org': 0.36; 'created': 0.36; 'charset:us-ascii': 0.36; 'data': 0.37; 'subject:: ': 0.38; 'files': 0.38; 'skip:l 20': 0.38; 'delete': 0.38; 'to:addr:python.org': 0.39; 'header:Received:5': 0.40; 'more': 0.63; 'fin': 0.84; 'dennis': 0.91; 'received:108': 0.91
X-Injected-Via-Gmane http://gmane.org/
To python-list@python.org
From Dennis Lee Bieber <wlfraed@ix.netcom.com>
Subject Re: Split single file into multiple files based on patterns
Date Wed, 24 Oct 2012 01:38:42 -0400
Organization > Bestiaria Support Staff <
References <9ae3b43a-5c69-4232-a1cf-e2dd0c4ee2ca@googlegroups.com> <CANy1k1jiHafh-mmhau158dAa3b6wPcfxv08igiT1W=hhAeBZWA@mail.gmail.com>
Mime-Version 1.0
Content-Type text/plain; charset=us-ascii
Content-Transfer-Encoding 7bit
X-Gmane-NNTP-Posting-Host adsl-108-79-220-228.dsl.klmzmi.sbcglobal.net
X-Newsreader Forte Agent 3.3/32.846
X-No-Archive YES
X-BeenThere python-list@python.org
X-Mailman-Version 2.1.15
Precedence list
List-Id General discussion list for the Python programming language <python-list.python.org>
List-Unsubscribe <http://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe>
List-Archive <http://mail.python.org/pipermail/python-list/>
List-Post <mailto:python-list@python.org>
List-Help <mailto:python-list-request@python.org?subject=help>
List-Subscribe <http://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe>
Newsgroups comp.lang.python
Message-ID <mailman.2720.1351057129.27098.python-list@python.org> (permalink)
Lines 40
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1351057129 news.xs4all.nl 6946 [2001:888:2000:d::a6]:34610
X-Complaints-To abuse@xs4all.nl
Xref csiph.com comp.lang.python:31996

Show key headers only | 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