mardi 25 avril 2017

Using Wildcard in CSV lookup with if/else statement in python

This bit of code is from a python script I have that will search for the employee id column of an employee in a CSV file, then output it if it exists in the column.

What I am trying to do is, if anyone changes the column order in the CSV file, to throw me an error that Column 3 is no longer the Employee ID Column by checking the header of the column, in the csv file, to see if it contains part of the phrase Employee ID by using a wildcard.

import csv

with open('Report1.csv', 'rb') as user_file:
    reader = csv.reader(user_file)

    for column in reader:
        print column[2]
        employeeIid = column[2]

        if column[2] == 'Employee ID':
            print employeeIid
        else:
            print "Employee ID Column in CSV changed-please repair"

When I run the above code I get this:

Employee ID
123456
Employee ID Column in CSV changed-please repair

It prints the employee ID, as well as the warning error. How can i improve/fix my code?

Please note: that the Employee ID is Column 3 in the CSV file(not 2- bit i assume python use 0 as a start).

Also, How can I Disregard the header, and only print the Employee ID number (ex 123456) instead of the Header AND the actual number?

Aucun commentaire:

Enregistrer un commentaire