Tuesday, 15 June 2010

How to merge two text files around a common value in python -



How to merge two text files around a common value in python -

i have 2 different files.

i need merge these 1 file. there mutual value. 2 files have format. matches not in sequence. dataset1 line1 may not match dataset2 line1. more dataset1 line1 match dataset2 line16 or line 45.

bold matching values. directional help appreciated.

beec,be-ec,,154.7,46.07,,31.63,54.6,4833.6,5.06 bplz,be-lz,,390.6,62.62,,49.0,145.0,27.3,61.52 bflp,bf-op,,180.1,34.89,,40.0,58.26,8533.8,7.31 mrm1234-beec-1635753e001 25.6 70.29 mrm1234-bplz-1814737e003 8.12 18.13 mrm1234-bflp-2470883e001 12.92 18.8

i know how utilize line.split array of each element.

i know how count first column l[6:4] of sec info set matching 4 letter value.

i've tried several ways suggested have not succeeded.

how merge columns in single row joined unique 4 digit identifier? matching of unique value , writing 1 line eludes me.

contents of file dat1:

beec,be-ec,,154.7,46.07,,31.63,54.6,4833.6,5.06 bplz,be-lz,,390.6,62.62,,49.0,145.0,27.3,61.52 bflp,bf-op,,180.1,34.89,,40.0,58.26,8533.8,7.31

contents of file dat2:

mrm1234-beec-1635753e001 25.6 70.29 mrm1234-bplz-1814737e003 8.12 18.13 mrm1234-bflp-2470883e001 12.92 18.8

use quick & dirty script concatenate lines of both files described.

dat1 = {} open('dat1') f: line in f.readlines(): dat1[line.split(',')[0]] = line.strip().split(',')[1:] dat2 = {} open('dat2') f: line in f.readlines(): key = line.strip().split()[0].split('-')[1] dat2[key] = line.strip().split()[1:] key in dat1.keys(): print("%s,%s,%s" % (key, str.join(',', dat1[key]), str.join(',', dat2[key])))

this produce next output.

bflp,bf-op,,180.1,34.89,,40.0,58.26,8533.8,7.31,12.92,18.8 beec,be-ec,,154.7,46.07,,31.63,54.6,4833.6,5.06,25.6,70.29 bplz,be-lz,,390.6,62.62,,49.0,145.0,27.3,61.52,8.12,18.13

python python-3.x

No comments:

Post a Comment