-3

I want to use file1.csv as a lookup for file2.csv. Anything that comes up should print the entire row from file2.csv.

2 sample data files

However, as I loop trough the rows of file2.csv and evaluate my lookup to my data I am unable to get the line variable to equal my second column (row1). What do I appear to be missing?

import csv
import sys

file1 = 'file1.csv'
file2 = 'file2.csv'

appcode = []
with open(file1, "r") as f:
    f.readline()            # Skip the first line
    for line in f:
        appcode.append(str(line.strip("\n")))
        print('This is what we are looking for from file1 ...' +line)
        csv_file = csv.reader(open(file2, "rb"), delimiter=",")   # was rb

        #loop through csv list
        for row in csv_file:
            print('line = '+line +'   '+'row is... '+row[1])

            #if current rows 2nd value is equal to input, print that row
            if str(line) is str(row[1]):
                print row
            else:
                print 'thinks '+str(line)+'='+str(row[1])+' is false'
Patrick Artner
  • 50,409
  • 9
  • 43
  • 69
Clip
  • 1
  • 1
  • 2
    Use `==` to chck for equality, not `is`, which tests whether two objects are the same object – snakecharmerb Dec 20 '18 at 16:40
  • ... what does your title mean? I have no clue... – Giacomo Alzetta Dec 20 '18 at 16:42
  • Thanks for the immediate feedback. #if current rows 2nd value is equal to input, print that row if str(line) == str(row[1]): print row else: print 'thinks '+str(line)+'='+str(row[1])+' is false' '==' still provides false information. And to answer Giacomo's question, my result of 1 does not equal 1. All results are showing False. – Clip Dec 20 '18 at 18:07

1 Answers1

0

You can restructure your code a bit:

import csv
import sys

file1 = 'file1.csv'
file2 = 'file2.csv'

# create your files data:
with open(file1,"w") as f:
    f.write("""appname\n1\neee\naaa\n""")
with open(file2,"w") as f:
    f.write("""server,appname\nfrog,1\napple,aaa\n""")


# process files
with open(file1, "r") as f:
    f.readline()            # Skip the first line
    f_set = set(  (line.strip() for line in f) )
    # no need to indent the rest as well - you are done with reading file1

print('This is what we are looking for from file1 ...', f_set)

with open(file2,"r") as f:
    csv_file = csv.reader(f, delimiter=",")  
    #loop through csv list
    for row in csv_file:
        if row[1] in f_set: # check for a in b is fastest with sets
            print(row)
        else:
            pass  # do something else

By checking row[1] in f_set you avoid the wrong comparison using is - as a general rule: use is only if you really want to check if 2 things are identical object - not if they containt the same things.

Output (2.7): # remove the ( and ) at print to make it nicer

('This is what we are looking for from file1 ...', set(['1', 'eee', 'aaa']))
['frog', '1']
['apple', 'aaa']

Output (3.6):

This is what we are looking for from file1 ... {'1', 'aaa', 'eee'}
['frog', '1']
['apple', 'aaa']

Readup:

https://docs.python.org/2/library/sets.html

Patrick Artner
  • 50,409
  • 9
  • 43
  • 69
  • This works great! Additionally I like the smooth move creating the data files on the fly. Thank you, and happy holidays, All. – Clip Dec 20 '18 at 18:26