Text file extract

Lutz Horn lutz.horn at fiz-karlsruhe.de
Fri Sep 5 03:17:49 EDT 2003


Hi Andy,

* "Andy" <Hoosbruin at Kconline.com> [05 Sep 2003]:
> 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.

To split a string use it's split() method. If the records are in a file 
named "records.txt" and for each distinct value of the third field all 
matching records should be written to a file file named as the value 
with ".txt" appended, I'd do something like:


# read all records from the input file
records = open("records.txt").readlines()

# create a dict to hold the output files. value => file
outfiles = {}

# loop over all records
for record in records:
    # split the record and extract the third field (index 2)
    field3 = record.split(",")[2]
    
    # if the value of the field hasn't been encountered before,
    # open an output file and store it in the dict using
    # the value as the key.
    if field3 not in outfiles.keys():
        outfile = open(field3 + ".txt", "w")
        outfiles[field3] = outfile

    # write the record to the proper output file
    outfiles[field3].write(record)

# clos all output files
for outfile in outfiles.values():
    outfile.close()


With your data this produces three files: BILL.txt, TIM.txt, and 
TOM.txt containing 1, 1, and 2 records.

I leave it to you to open the files in proper subdirectories :-)

Regards
Lutz
-- 
no sig




More information about the Python-list mailing list