473,396 Members | 2,090 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Filtering two files with uncommon column

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
Jan 18 '08 #1
3 1298
On Jan 18, 11:23 am, Madhur <madhurr...@gmail.comwrote:
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
Jan 18 '08 #2
"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

Jan 18 '08 #3
On Jan 18, 2008 4:23 AM, Madhur <ma********@gmail.comwrote:
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***************@gmail.com>
Jan 18 '08 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: N.K. | last post by:
Hello, I'm trying to find a way to flexibly filter a column in a row of data read from a file. I will have data type of that column, the operation to be performed on it (like >, <, <= etc.) and the...
1
by: Alex Satrapa | last post by:
I have a table from which I'm trying to extract certain information. For historical reasons, we archive every action on a particular thing ('thing' is identified, funnily enough, by 'id'). So the...
19
by: William Wisnieski | last post by:
Hello Everyone, I have a main form with a datasheet subform that I use to query by form. After the user selects two criteria on the main form and clicks the cmdShowResults button on the main...
2
by: Sean | last post by:
Greetings all, I am attempting to make a form that will filter through several tables that (I believe) have refretial integrity. I am pulling data from several tables into the form and i would...
7
by: | last post by:
Hello, Does anyone have an idea on how I can filter the data in the gridview control that was returned by an sql query? I have a gridview that works fine when I populate it with data. Now I...
2
by: JUAN ERNESTO FLORES BELTRAN | last post by:
Hi you all, I am developping a python application which connects to a database (postresql) and displays the query results on a treeview. In adittion to displaying the info i do need to implement...
2
by: kevinpond | last post by:
I have a Gridview displaying the results of a stored procedure. The stored procedure is constantly changing so the columns displayed in the data grid are constantly changing. I'd like to give...
3
by: Harry Haller | last post by:
Hello, I want to implement a generic list which will be used to display 7 columns in a GridView. One should be able to sort, filter and page each of the 7 columns. Ideally the filter should be...
5
by: RHooper | last post by:
Hi, I'm new to Access, so I apologize if this question is trivial. I am trying to set-up a quick filter for users to define on a form bound to a table. I have a combo box called...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.