3

All -

I am attempting to read a single row from a csv file and then have it search another csv file.

I have a masterlist.csv that has a single column called empID. It contains thousands of rows of 9 digit numbers. As well I have ids.csv that also contains a single column called number. It contains hundreds of rows. I am attempting to pull a row from the ids.csv do a search on the masterlist.csv and print out whether it has been found. Then it needs to move to the next row in ids.csv until each row in ids.csv has been searched within the masterlist.csv. I thought it would be as simple as this, however it is not throwing any errors nor returning any results.

Using Python 2.7.12 import csv

masterReader = csv.reader(open("masterlist.csv", "rt"), delimiter=",")
idsReader = csv.reader(open("ids.csv", "rt"), delimiter=",")


for number in idsReader:
    for empID in masterReader:
        if number == empID:
            print (" Found in MasterFile")
        else:
            print ("Is Not Found in MasterFile")

Edit: Adding snippet of data used for testing.

enter image description here

enter image description here

3
  • see the docs for reeading csv files, it appears that your syntax is wrong: docs.python.org/2/library/csv.html "with open('ids.csv', 'rb') as csvfile:" for example...at least I have had probkems the past when not opening them this way... Commented Sep 13, 2016 at 18:53
  • Can you provide a snip of the input? I would suspect a .strip() may solve the issue. Also, you can have it print number and empID to ensure you're getting what you think you're getting. Commented Sep 13, 2016 at 18:55
  • That doesn't look like it is comma separated to me (unless np++ is mangling it for you) Commented Sep 13, 2016 at 19:14

2 Answers 2

1

Content of master.csv

EmpId
111111111
222222222
333333333
444444444

Content of ids.csv:

Number
111111111
999999999
444444444
555555555
222222222

Code:

import csv

f1 = file('master.csv', 'r')
f2 = file('ids.csv', 'r')

c1 = csv.reader(f1)
c2 = csv.reader(f2)

idlist = list(c2)
masterlist = list(c1)

for id in idlist[1:]:
    found = False
    #Need to ignore heading thats why masterlist[1:]
    for master in masterlist[1:]:
        if id == master:
            found = True
    if found:
        print "Found in master file"
    else:
        print "Not found in master file"

f1.close()
f2.close()

Output:

C:\Users\dinesh_pundkar\Desktop>python c.py
Found in master file
Not found in master file
Found in master file
Not found in master file
Found in master file

C:\Users\dinesh_pundkar\Desktop>

More shorter version of code without CSV module

with open('master.csv','r') as master:
    with open('ids.csv','r') as id:
        id_list = id.readlines()[1:]
        master_list = master.readlines()[1:]
        for id in id_list:
            if id in master_list:
                print "Found in master file"
            else:
                print "Not found in master file"
Sign up to request clarification or add additional context in comments.

Comments

0

You could easily find the numbers that are common to both by using the intersection of sets and the csv.DictReader as your reader object (not sure if the file actually contains a single column):

with open("masterlist.csv") as f1, open("ids.csv") as f2:
    masterReader = csv.DictReader(f1)
    idsReader = csv.DictReader(f2)

    common = set(row['empID'] for row in masterReader) \
           & set(row['number'] for row in idsReader)
    print(common)

Or use a check of list membership to find rows in idsReader that are contained in masterReader:

masterReader = [row['empID'] for row in masterReader]

for row in idsReader:
    if row['number'] in masterReader:
        print (" Found in MasterFile")
    else:
        print ("Is Not Found in MasterFile")

P.S. considering the update to your question, you may not even need the csv module to do this

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.