nrcombs
nrcombs

Reputation: 533

print lines from file based on items in list

I'm stuck on some python code and wondering if anyone might be able to point me in the right direction. I have two text files that are structured as follows:

file1

48.1.a  48.1
48.1.b  48.1
48.1.c  48.1
49.1.a  49.1
50.1.a  50.1
50.1.b  50.1
50.1.c  50.1

file2

48.1   info1   info2   info3
49.1   info1   info2   info3
50.1   info1   info2   info3

Note that I have duplicate entries in file1 column 2, and these correspond with unique entries in file2 column 1. For each line in file1, I would like to print the line from file2 that corresponds with the value in the second column of file1, such that:

48.1   info1   info2   info3
48.1   info1   info2   info3
48.1   info1   info2   info3
49.1   info1   info2   info3
50.1   info1   info2   info3
50.1   info1   info2   info3

I've attempted the following code, which includes using a list to retain duplicate values from file1:

list=[]
with open("file1.txt") as file1:
    for i in file1:
        list.append(i.strip().split()[1])

with open("file2.txt") as file2:
    for j in list:
        for k in file2:
            item = k.strip().split()[0]
            if item==j:
                print(k.strip(), file=output)

This code does not give me the desired output, I think because of my nested for loops (which may also be bad coding practice?) and how I am iterating over the items in list and the lines in file2. Thanks for any help!

Upvotes: 0

Views: 62

Answers (3)

Paul Rooney
Paul Rooney

Reputation: 21609

As described in comments. You can make a dictionary of the entries in file 2. Then use them to look up the entries based on the second column in file 1.

def mkentry(line):

    first, rest = line.split(None, 1)

    return first, rest.split()


with open("file2.txt") as file2:

    mapping = dict(mkentry(line) for line in file2)


with open('file1.txt') as file1:

    for line in file1:

        col1, col2 = line.split()

        if col2 in mapping:
            print('%s %s' % (col2, ' '.join(mapping[col2])))
        else:
            print('%s no entries' % col2)

result:

48.1 info1 info2 info3
48.1 info1 info2 info3
48.1 info1 info2 info3
49.1 info1 info2 info3
50.1 info1 info2 info3
50.1 info1 info2 info3
50.1 info1 info2 info3

Upvotes: 1

nkaushik
nkaushik

Reputation: 71

You can enter use the first column of file 2 as keys and their rows as values. Then it should be easy to access the values from the second column in file 1.

Something like the code below should work.

entry_dict = {}

with open('f2.txt') as f2:
    lines = f2.readlines()
    for line in lines:
        l = line.split()
        entry_dict[l[0]] = l[1:] 

with open('f1.txt') as f1:
    lines = f1.readlines()
    for line in lines:
        item = line.split()[1]
        print(' '.join([item] + entry_dict[item]))

Upvotes: 1

Merig
Merig

Reputation: 2011

Since the values in file2 are unique, I would use them as keys in a dictionary, which can be used to retrieve the informations you want.

dict_info = {}

with open('file2.txt') as f:
    for row in f.readlines():
        value, info = row.split()[0], ' '.join([row.split()[i] for i in range(1, 4)])
        dict_info[value] = info

with open('file1.txt') as f:
    for row in f.readlines():
        value = row.split()[1]
        print(value, dict_info[value])

Upvotes: 1

Related Questions