2

I am very new to Python but I'm trying to learn more and my first mini project that I've given myself is to create a code that can receive the input for a given user name and then output the amount of hours they worked on a certain project. I receive a csv at the end of the week for employees in my department and within that csv I have various projects that they are working on and the hours dedicated to that project. The catch with the csv file is there are duplicate projects for that user so when my csv outputs I need it to only show one project name and ALL the hours associated with that project. How can I get my code to read the duplicates and only count the hours from the duplicates and just use one project name?

Here is the code I've come up with so far:

import csv
with open('Report.csv') as csvfile:
    readCSV = csv.reader(csvfile, delimiter=',')
    firsts = []
    lasts = []
    projects = []
    hours = []
    for row in readCSV:
        first = row[0]
        last = row[1]
        project = row[2]
        hour = row[3]

        firsts.append(first)
        lasts.append(last)
        projects.append(project)
        hours.append(hour)

    First_name = input("Please enter the first name: ")
    First_1 = firsts.index(First_name)
    Last_1 = lasts[First_1]
    project_1 = projects[First_1]
    hours_1 = hours[First_1]
    print(First_1, Last_1, project_1, hours_1)

Here is a sample of the csv

First   Last    Project             Hours
David   Ayers   AD-0002  Training   24
Oriana  Morfitt AD-0002  Training   24
David   Ayers   AD-0003  Personal Time  8
David   Ayers   AD-0004  Sick Time  0
Oriana  Morfitt AD-0005  Vacation   40
Sujatha Kavuri  Beeline Blank   29
Sujatha Kavuri  Beeline Blank   16
Sujatha Kavuri  OPS-0001 General Operational Support    6
Jeff    Moore   OPS-0001 General Operational Support    5
Sri Mantri  SRV-0001 Service Requests for Base and Direct Services  4
Prasanth    Musunuru    SRV-0001 Service Requests for Base and Direct Services  11
Prasanth    Musunuru    SRV-0001 Service Requests for Base and Direct Services  10
Jeff    Moore   SRV-0006 Standards and Processes    5
Jeff    Moore   SRV-0006 Standards and Processes    3
Jeff    Moore   SRV-2503 Internet Access Infrastructure Maintenance & Support   12.5
Jeff    Moore   SRV-2503 Internet Access Infrastructure Maintenance & Support   7
Jeff    Moore   0024495915 Work Instructions (infrastructure) - time tracking   1
Sri Mantri  0026184229 Margin Controlling Java Rewrite  4
Sujatha Kavuri  0029157489 SCRM Life Cycle Extension    3
Jeff    Moore   0031369443 Shopcall Attachment Changes  1
Jeff    Moore   0031500942 MP Strategy 2015 - Spot PO via EDI (time tracking only) - 0031500942 1

1 Answer 1

2

I bet there's a better way of doing it with pandas, but this will work too:

import csv

# I've used full name to avoid duplicate first names in report 
full_name = input('Enter your full name: ')

with open('Report.csv') as csvfile:
    hour_summation = {}
    read_csv = csv.reader(csvfile, delimiter=',')
    for row in read_csv:
        if ' '.join((row[0], row[1])) == full_name.strip():
            hour_summation[row[2]] = hour_summation.get(row[2], 0) + int(row[3])

print('This is {} full hours report:'.format(full_name))
for k, v in hour_summation.items():
    print(k + ': ' + str(v) + ' hours')

results for Sujatha Kavuri

Enter your full name: Sujatha Kavuri
This is Sujatha Kavuri full hours report:
Beeline Blank: 45 hours
OPS-0001 General Operational Support: 6 hours

EDIT - I've only sampled half the file so the results aren't complete.
Hope this helps.

Sign up to request clarification or add additional context in comments.

8 Comments

This looks perfect but for some reason It's not working for me? are you using an early version of python?
I'm using version 3.5. What error are you getting ?
It's not even an error its just only getting to this point: Enter your full name: Sujatha Kavuri This is Sujatha Kavuri full hours report. I am also using 3.5
Are you sure your csv file is formatted with a comma delimiter ? because in your sample it's using space as a delimiter but when loading it with csv.reader your using a comma as a delimiter. In my sample I had to reformat it with a comma
I figured it out my problem. Your solution worked great!
|

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.