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


Groups > comp.lang.python > #32021

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!newsfeed5.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.001
X-Spam-Evidence '*H*': 1.00; '*S*': 0.00; 'operator': 0.03; 'subject:file': 0.07; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'subject:files': 0.09; 'subject:into': 0.09; '"a")': 0.16; '303': 0.16; '3465': 0.16; 'itemgetter': 0.16; 'itertools': 0.16; 'pairs': 0.16; 'received:80.91.229.3': 0.16; 'received:dip.t-dialin.net': 0.16; 'received:plane.gmane.org': 0.16; 'received:t-dialin.net': 0.16; 'subject:based': 0.16; 'wrote:': 0.17; 'import': 0.21; 'split': 0.23; 'header:User- Agent:1': 0.26; 'first.': 0.27; 'header:X-Complaints-To:1': 0.28; 'run': 0.28; 'key,': 0.29; 'code': 0.31; 'file': 0.32; 'running': 0.32; 'to:addr:python-list': 0.33; 'text': 0.34; 'thanks': 0.34; 'path': 0.35; 'received:org': 0.36; 'previous': 0.37; 'subject:: ': 0.38; 'files': 0.38; 'skip:o 20': 0.38; 'sure': 0.38; 'to:addr:python.org': 0.39; 'where': 0.40; 'header:Received:5': 0.40; 'remove': 0.61; 'more': 0.63; '2995': 0.84; 'group)': 0.84
X-Injected-Via-Gmane http://gmane.org/
To python-list@python.org
From Peter Otten <__peter__@web.de>
Subject Re: Split single file into multiple files based on patterns
Date Wed, 24 Oct 2012 10:17:39 +0200
Organization None
References <9ae3b43a-5c69-4232-a1cf-e2dd0c4ee2ca@googlegroups.com>
Mime-Version 1.0
Content-Type text/plain; charset="ISO-8859-1"
Content-Transfer-Encoding 7Bit
X-Gmane-NNTP-Posting-Host p50848f8e.dip.t-dialin.net
User-Agent KNode/4.7.3
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.2742.1351066646.27098.python-list@python.org> (permalink)
Lines 46
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1351066646 news.xs4all.nl 6886 [2001:888:2000:d::a6]:56048
X-Complaints-To abuse@xs4all.nl
Xref csiph.com comp.lang.python:32021

Show key headers only | View raw


satyam 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

import os
from itertools import groupby
from operator import itemgetter

get_key = itemgetter(0)
get_value = itemgetter(1)

output_folder = "tmp"
with open("infile.txt") as instream:
    pairs = (line.split(None, 1) for line in instream)
    for key, group in groupby(pairs, key=get_key):
        path = os.path.join(output_folder, key + ".txt")
        with open(path, "a") as outstream:
            outstream.writelines(get_value(line) for line in group)

If you are running the code more than once make sure that you remove the 
files from the previous run first.

Back to comp.lang.python | Previous | NextPrevious 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