4

I'm trying to print all data from a csv in lowercase, but I'm not having any luck.

Here's what I have so far:

import csv
books = csv.reader(open("books.csv","rb"))

for row in books:
    print row

This prints all the content of the csv, but when I add the .lower() function, I get errors.

What am I doing wrong?

1 Answer 1

7

Try

print [r.lower() for r in row]
Sign up to request clarification or add additional context in comments.

3 Comments

Thanks Dogbert! Question - what exactly does the "r" do in this case?
It's similar to your for row in books statement. You can change it to any variable name.
to make it clear it's equivalent to print [item.lower() for item in row] it's just a variable name local to the list comprehension expression (If I'm correct it's local in Python 3.x and declarated after in Python 2.x)

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.