[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.python

Filtering two files with uncommon column

Madhur

1/18/2008 9:23:00 AM

I would like to know the best way of generating filter of two files
based upon the following condition

I have two files. Contents of the first file is

File 1
abc def hij
asd sss lmn
hig pqr mno


File 2

jih def asd
poi iuu wer
wer pqr jjj

I would like have the output as
Output

File1
asd sss lmn
File2
poi iuu wer

Basically I want to compare the two files based on second column. If
the second
column matches on both the files do not print anything, else if there
is no matc
h in for the second column for first file in second file then print it
under Fil
e1 header, else if there is no match for the second column for second
file in fi
rst file print it under File2 header.

Thankyou
Madhur
12 Answers

Chris

1/18/2008 9:38:00 AM

0

On Jan 18, 11:23 am, Madhur <madhurr...@gmail.com> wrote:
> I would like to know the best way of generating filter of two files
> based upon the following condition
>
> I have two files. Contents of the first file is
>
> File 1
> abc def hij
> asd sss lmn
> hig pqr mno
>
> File 2
>
> jih def asd
> poi iuu wer
> wer pqr jjj
>
> I would like have the output as
> Output
>
> File1
> asd sss lmn
> File2
> poi iuu wer
>
> Basically I want to compare the two files based on second column. If
> the second
> column matches on both the files do not print anything, else if there
> is no matc
> h in for the second column for first file in second file then print it
> under Fil
> e1 header, else if there is no match for the second column for second
> file in fi
> rst file print it under File2 header.
>
> Thankyou
> Madhur

file1 = open('file1.txt','rb')
file2 = open('file2.txt','rb')

file1_line = file1.next()
file2_line = file2.next()

while file1_line and file2_line:
try:
f1_col2 = file1_line.split(' ')[1]
except IndexError:
print 'Not enough delimiters in line.'
try:
f2_col2 = file2_line.split(' ')[2]
except IndexError:
print 'Not enough delimiters in line.'

if f1_col2 != f2_col2:
outfile_data_to_relevant_files()

file1_line = file1.next()
file2_line = file2.next()

HTH
Chris

Madhur

1/18/2008 10:08:00 AM

0

On Jan 18, 2:37 pm, Chris <cwi...@gmail.com> wrote:
> On Jan 18, 11:23 am, Madhur <madhurr...@gmail.com> wrote:
>
>
>
> > I would like to know the best way of generating filter of two files
> > based upon the following condition
>
> > I have two files. Contents of the first file is
>
> > File 1
> > abc def hij
> > asd sss lmn
> > hig pqr mno
>
> > File 2
>
> > jih def asd
> > poi iuu wer
> > wer pqr jjj
>
> > I would like have the output as
> > Output
>
> > File1
> > asd sss lmn
> > File2
> > poi iuu wer
>
> > Basically I want to compare the two files based on second column. If
> > the second
> > column matches on both the files do not print anything, else if there
> > is no matc
> > h in for the second column for first file in second file then print it
> > under Fil
> > e1 header, else if there is no match for the second column for second
> > file in fi
> > rst file print it under File2 header.
>
> > Thankyou
> > Madhur
>
> file1 = open('file1.txt','rb')
> file2 = open('file2.txt','rb')
>
> file1_line = file1.next()
> file2_line = file2.next()
>
> while file1_line and file2_line:
> try:
> f1_col2 = file1_line.split(' ')[1]
> except IndexError:
> print 'Not enough delimiters in line.'
> try:
> f2_col2 = file2_line.split(' ')[2]
> except IndexError:
> print 'Not enough delimiters in line.'
>
> if f1_col2 != f2_col2:
> outfile_data_to_relevant_files()
>
> file1_line = file1.next()
> file2_line = file2.next()
>
> HTH
> Chris

If the files2 is unordered, then the above logic does not work. How to
takle it?

Paul Rubin

1/18/2008 10:56:00 AM

0

Madhur <madhurrajn@gmail.com> writes:
> If the files2 is unordered, then the above logic does not work. How to
> takle it?

This sounds like a homework problem. Also, you are trying to reimplement
the unix "comm" command.

Chris

1/18/2008 11:21:00 AM

0

On Jan 18, 12:08 pm, Madhur <madhurr...@gmail.com> wrote:
> On Jan 18, 2:37 pm, Chris <cwi...@gmail.com> wrote:
>
>
>
> > On Jan 18, 11:23 am, Madhur <madhurr...@gmail.com> wrote:
>
> > > I would like to know the best way of generating filter of two files
> > > based upon the following condition
>
> > > I have two files. Contents of the first file is
>
> > > File 1
> > > abc def hij
> > > asd sss lmn
> > > hig pqr mno
>
> > > File 2
>
> > > jih def asd
> > > poi iuu wer
> > > wer pqr jjj
>
> > > I would like have the output as
> > > Output
>
> > > File1
> > > asd sss lmn
> > > File2
> > > poi iuu wer
>
> > > Basically I want to compare the two files based on second column. If
> > > the second
> > > column matches on both the files do not print anything, else if there
> > > is no matc
> > > h in for the second column for first file in second file then print it
> > > under Fil
> > > e1 header, else if there is no match for the second column for second
> > > file in fi
> > > rst file print it under File2 header.
>
> > > Thankyou
> > > Madhur
>
> > file1 = open('file1.txt','rb')
> > file2 = open('file2.txt','rb')
>
> > file1_line = file1.next()
> > file2_line = file2.next()
>
> > while file1_line and file2_line:
> > try:
> > f1_col2 = file1_line.split(' ')[1]
> > except IndexError:
> > print 'Not enough delimiters in line.'
> > try:
> > f2_col2 = file2_line.split(' ')[2]
> > except IndexError:
> > print 'Not enough delimiters in line.'
>
> > if f1_col2 != f2_col2:
> > outfile_data_to_relevant_files()
>
> > file1_line = file1.next()
> > file2_line = file2.next()
>
> > HTH
> > Chris
>
> If the files2 is unordered, then the above logic does not work. How to
> takle it?

Take a look at *nix's sort command, it can also sort based on a key

mblume

1/18/2008 12:29:00 PM

0

"Madhur" schrieb
> I would like to know the best way of generating filter
> of two files based upon the following condition
> [...]
>
Sounds like homework. Here some suggestions:

- for each file, create a dictionary (see help(dict)
in the python shell for details) and populate it with
the values, so that e.g.
d1['def'] = 'abc def hij'
(help("".split), perhaps help("".strip))

- for each key in the first dictionary, look whether
it exists in the second, if not, write the value (the
line extracted in the first step) out.
(help(dict.iteritems), help(dict.has_key))
(Note that for
if a_dict.has_key("def"): pass
one can also write
if "def" in a_dict: pass
but you won't find this in the simple on-line help,
at least in my version)

HTH
Martin

Neil Cerutti

1/18/2008 3:13:00 PM

0

On Jan 18, 2008 4:23 AM, Madhur <madhurrajn@gmail.com> wrote:
> I would like to know the best way of generating filter of two files
> based upon the following condition

As a bit of friendly advice, you'll get much more useful assistance if
you post your code.

If you don't have any code to show, write some. Unless it's a quine, a
program won't write itself.

--
Neil Cerutti <mr.cerutti+python@gmail.com>

Reedick, Andrew

1/18/2008 6:26:00 PM

0

> -----Original Message-----
> From: python-list-bounces+jr9445=att.com@python.org [mailto:python-
> list-bounces+jr9445=att.com@python.org] On Behalf Of Madhur
> Sent: Friday, January 18, 2008 4:23 AM
> To: python-list@python.org
> Subject: Filtering two files with uncommon column
>
>
> Basically I want to compare the two files based on second column. If
> the second
> column matches on both the files do not print anything, else if there
> is no matc
> h in for the second column for first file in second file then print it
> under Fil
> e1 header, else if there is no match for the second column for second
> file in fi
> rst file print it under File2 header.
>


I often do this to compare property files between environments. The
follow algorithm works for any number of files by creating a dictionary
of lists (or hash of arrays in Perl-ese.)

Create a dictionary
Index = -1
For file in files
Index++
For line in file
col = match/split/regex the column
If col not in dictionary
Dictionary[col] = []

extend dictionary[col] to length of index
dictionary[col][index] = col

for col in sort(dictionary.keys()):
extend dictionary[col] to length of index
print dictionary[col]




*****

The information transmitted is intended only for the person or entity to which it is addressed and may contain confidential, proprietary, and/or privileged material. Any review, retransmission, dissemination or other use of, or taking of any action in reliance upon this information by persons or entities other than the intended recipient is prohibited. If you received this in error, please contact the sender and delete the material from all computers. GA622


Dave Kelly

6/29/2011 11:38:00 PM

0


"Sherry in Vermouth" <sherry13@together.net> wrote in message

> Yah, yah... you always have an excuse!
>
> Sherry in Vermouth

You should immediately thank your lucky stars
I'm busy that weekend....
I'd be my first and your last bonfire.
TRUST me on that, girlfren.


Sherry in Vermont

6/30/2011 5:20:00 PM

0

On 2011-06-29 11:49:35 -0400, Edwin Hurwitz <edwin@indra.com> said:

> In article <2011062900593250878-sherry13@togethernet>,
> Sherry in Vermont <sherry13@together.net> wrote:
>
>> On 2011-06-28 17:55:19 -0400, "sweetbac" <sweetbac@sbcglobal.net> said:
>>
>>>
>>> "Sherry in Vermouth" <sherry13@together.net> wrote in message
>>>
>>>> And strangely, for such a horribly awful event, damn near everyone comes
>>>> back year after year. Sherry in Vermouth
>>>
>>> <STILL waiting for an invitation>
>>
>> Oh, yah right! I even invited your mule and sherpa! Advised against the
>> aardvark, though, as my huskies would consider it a most interesting
>> snack.
>>
>> I think you're SKEEERED of Vermont! ;P
>>
>> But hey Sweetie, this year it's August 13th... consider youself invited!
>>
>> Sherry in Vermont
>
> Damn, I'll miss it by one week. I'm in Vermont on the 5th and 6th.

I will be enroute home from China on the 5th and 6th... hoping I am
over jetlag by the bonfire!

Sherry in Vermont

Sherry in Vermont

6/30/2011 5:38:00 PM

0

On 2011-06-29 19:37:43 -0400, "sweetbac" <sweetbac@sbcglobal.net> said:

>
> "Sherry in Vermouth" <sherry13@together.net> wrote in message
>
>> Yah, yah... you always have an excuse!
>>
>> Sherry in Vermouth
>
> You should immediately thank your lucky stars
> I'm busy that weekend....
> I'd be my first and your last bonfire.
> TRUST me on that, girlfren.

<snort> You're all talk!

Sherry in Vermont