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


Groups > comp.lang.python > #31995

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 <demianbrecht@gmail.com>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.006
X-Spam-Evidence '*H*': 0.99; '*S*': 0.00; 'subject:file': 0.07; "'w')": 0.09; 'subject:files': 0.09; 'subject:into': 0.09; 'url:github': 0.09; 'cc:addr:python-list': 0.10; 'closes': 0.16; 'count,': 0.16; 'count.': 0.16; 'subject:based': 0.16; 'wrote:': 0.17; 'cc:2**0': 0.23; 'cc:no real name:2**0': 0.24; 'cc:addr:python.org': 0.25; 'header:In-Reply-To:1': 0.25; 'skip:@ 10': 0.27; 'block,': 0.29; 'file': 0.32; 'received:209.85.160.46': 0.32; 'received:google.com': 0.34; 'pm,': 0.35; 'received:192.168.0': 0.35; 'received:209.85': 0.35; 'but': 0.36; 'message-id:@gmail.com': 0.36; 'charset:us-ascii': 0.36; 'received:209': 0.37; 'subject:: ': 0.38; 'received:192': 0.39; 'received:192.168': 0.40; 'header:Received:5': 0.40; "you've": 0.61; 'header:Message-Id:1': 0.62
DKIM-Signature v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=content-type:mime-version:subject:from:in-reply-to:date:cc :content-transfer-encoding:message-id:references:to:x-mailer; bh=sEAEIqbiWTot2akw3X/u2+sl4j/udZUQSBV2Ekj2OvQ=; b=zLNkE58owUtgEAxV3sTvN7U74wqC8UV0eQIQj5hSYFSqMzKdAGIXBbLUqgwDBk4mhS CT+++R2Cagl/hKuUEl4vjdoB2Zp1wzJhqh8fQe9vHAJQ/WMVn+j1ATruQcxBDE2Y+bhJ sgbddX1fJX2v/Z3VfK4YtmF+eYlG/H6067pn5DKmnYCxmosf0lVmPYV4zJNhS0OSTU24 1lIBHrQ61n6tQFfKD4jqt6uQs42EHOHG/UqYTYteu+wYH9+WblruDcoh31V2RWkl4Cjz 0mOxOsGRpY4fl4J9HjpEzuK4qTfxyXq8ZuRkqoxfuIp3eCCysnRhECItb2drAF2PRPgP 44Fg==
Content-Type text/plain; charset=us-ascii
Mime-Version 1.0 (Mac OS X Mail 6.0 \(1485\))
Subject Re: Split single file into multiple files based on patterns
From Demian Brecht <demianbrecht@gmail.com>
In-Reply-To <CA+vVgJUx5sVHZnyTzqrQqYwa+8+ZFb-80oxv7LsmwF=Hs6xqhw@mail.gmail.com>
Date Tue, 23 Oct 2012 22:36:56 -0700
Content-Transfer-Encoding quoted-printable
References <9ae3b43a-5c69-4232-a1cf-e2dd0c4ee2ca@googlegroups.com> <CA+vVgJUx5sVHZnyTzqrQqYwa+8+ZFb-80oxv7LsmwF=Hs6xqhw@mail.gmail.com>
To David Hutto <dwightdhutto@gmail.com>
X-Mailer Apple Mail (2.1485)
Cc python-list@python.org
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.2719.1351057021.27098.python-list@python.org> (permalink)
Lines 28
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1351057021 news.xs4all.nl 6933 [2001:888:2000:d::a6]:33415
X-Complaints-To abuse@xs4all.nl
Xref csiph.com comp.lang.python:31995

Show key headers only | View raw


On 2012-10-23, at 10:24 PM, David Hutto <dwightdhutto@gmail.com> wrote:

> count = 0
Don't use count.

> for file_data in turn_text_to_txt:
Use enumerate: 

for count, file_data in enumerate(turn_text_to_txt):

> f = open('/home/david/files/%s_%s.txt' % (file_data.split(' ')[0], count), 'w')
Use with: 

with open('file path', 'w') as f:
    f.write('data')

Not only is it shorter, but it automatically closes the file once you've come out of the inner block, whether successfully or erroneously.


Demian Brecht
@demianbrecht
http://demianbrecht.github.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