-1

I am writing some python code to practice for an exam in January. I need to read the second column into my code and print it out. If possible i also need to add data to specific columns.

The code i have tried is:

def view_this_weeks_sales(): #name of function

with open('employees-names1.csv ') as data: #name of csv file
    reader = csv.reader(data)
    first_column  = next(zip(*reader))
    print first_column

My file is below, it has four columns. Thanks in advance :)

Name of employee,Number of cars sold last week, Number of cars sold this week,Number of cars sold total (all on one line)

1,7,7,14

2,7,8,15

3,9,2,11

4,8,6,14

5,2,9,11

6,4,15,19

Total, ,47,84

1
  • 1
    You say you're writing python code to practice, yet I see no code in your question. You shouldn't ask stackoverflow to solve your homework for you. Please provide the code you've written so far. Commented Dec 20, 2018 at 18:10

1 Answer 1

1

One way of doing this would be

import csv
#replace the name with your actual csv file name
file_name = "data.csv" 
f = open(file_name)
csv_file = csv.reader(f)
second_column = [] #empty list to store second column values
for line in csv_file:
    second_column.append(line[1])
    print(line[1]) #index 1 for second column 

Second_column variable will hold the necessary values. Hope this helps.

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

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.