user10029838
user10029838

Reputation:

Python way to compare the values from file A and B and if A matches then print Entire line from B

I have two text files:

myfile1:

sports1
sports2
sports3
sports4
sports5
sports6
sports7
sports8

myfile2:

sports1  Cricket
sports2  golf
sports3  Hocky
sports4
sports5  Chess
sports6  Snooker
sports7  Foosball
sports8  Tampts

From the two files above, f column one in myfile1 matches with column one in myfile2 then it should print both columns of myfile2.

The awk one-liner below works, but I am looking for a similar one in Python.

awk 'NR==FNR{c[$1]++;next};c[$1]' myfile1 myfile2

Upvotes: 1

Views: 66

Answers (2)

Ivan Calderon
Ivan Calderon

Reputation: 620

Just as an option, you could do the same with lists and dictionaries. Considering this thread, you can open files inside comprehensions, which will be closed after the execution. Although the best approach to such task is to use "with" as context manager.

f1 = [x.strip("\n") for x in open("myfile1.txt")].sort()
f2 = {x[0]: x[1] for x in [l.split() for l in open("myfile2.txt")]}
if f1 == list(f2.keys()).sort():
    [print(k, v) for k, v in f2.items()]

Upvotes: 0

jpp
jpp

Reputation: 164773

You can, if you wish, use a 3rd party library such as Pandas. This is likely as close as you'll get to your one-liner.

delim_whitespace=True ensure Pandas uses whitespace to separate your columns, as opposed to , which you would expect from a regular csv.

import pandas as pd

df1 = pd.read_csv('file1.txt', header=None, names=['sport'])
df2 = pd.read_csv('file2.txt', header=None, names=['sport', 'name'],
                  delim_whitespace=True)

res = df2[df2['sport'].isin(df1['sport'].unique())]

print(res)

Upvotes: 1

Related Questions