Text file extract

Brandon Beck bbeck at NOSPAM.austin.rr.com
Sun Sep 7 19:30:02 CEST 2003


I wrote a generalized version of this exact thing a year or so ago.  It
can be found at:
http://isomorphism.org/~bbeck/splitter/

Additionally a post I made about it from last year can be found at:
http://tinyurl.com/mjj6


Specifically you'll want to write something like what's contained in
FirstNameSplitter.py but for your CSV format.  Here's what I suggest you
make YourSplitter.py look something like.

--------

import re
re_group_name = re.compile('[^,]*,[^,]*,(?P<name>[^,]*),.*')

class YourSplitter:
    def get_group_name(self, line):
        return re_group_name.group("name")

    def get_group_filename(self, groupname):
        return groupname + '.txt'  #  You'll want to update this to
specify your subdirectory too

    def update_line(self, line):
        return line  # no need to modify the line

-------

Once that's done you can run 'python splitter.py YourSplitter input.txt'
and it should just work its magic.


"Andy" <Hoosbruin at Kconline.com> wrote in message
news:1PudndLL4fUZWMqiU-KYuA at kconline.com...
>
> I have a test file and need to split the data based upon a record in a
> certain field. Then write those record to a multiple files in
different
> folders.
>
>
>
>  Example data:
>
> RECORD 1)  ABCD,666,TOM,4.00,9/1/03
> RECORD 2)  TYTY,666,TIM,4.00,9/1/03
> RECORD 3)  ABCD,666,BILL,4.00,9/1/03
> RECORD 4)  XXXX,666,TOM,4.00,9/1/03
>
>
> All the fields will be comma separated and the 3rd field will contain
the
> record I will use to split.
>
>  So records 1 and 4 get written to the same file call it Cust.txt and
write
> it to folder \\server\tom\Cust.txt
>  Then record 2  gets written to cust.txt in a different folder
> \\server\tim\cust.txt
>
>
>   and so on.....
>
>  I have never written a Python script so would like to see how this
would be
> done. I deal with a lot of text file input and would like to see how
easy
> Python handles this task..
>
>  thanks in advance... If you could doc some of your code so I can
follow
> thru it .. would be great.
>
>
>
>
>
>
>
>
>
>
>
>
>






More information about the Python-list mailing list