Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using "with" statement for CSV files in Python

Tags:

Is it possible to use the with statement directly with CSV files? It seems natural to be able to do something like this:

import csv with csv.reader(open("myfile.csv")) as reader:     # do things with reader 

But csv.reader doesn't provide the __enter__ and __exit__ methods, so this doesn't work. I can however do it in two steps:

import csv with open("myfile.csv") as f:     reader = csv.reader(f)     # do things with reader 

Is this second way the ideal way to do it? Why wouldn't they make csv.reader directly compatible with the with statement?

like image 428
Kiv Avatar asked Jan 13 '09 22:01

Kiv


People also ask

Can Python work with CSV files?

Any language that supports text file input and string manipulation (like Python) can work with CSV files directly.

How do I write to a CSV file in Python?

Steps for writing a CSV file First, open the CSV file for writing ( w mode) by using the open() function. Second, create a CSV writer object by calling the writer() function of the csv module. Third, write data to CSV file by calling the writerow() or writerows() method of the CSV writer object.

What is the best way to read a CSV file in Python?

Read A CSV File Using Python There are two common ways to read a . csv file when using Python. The first by using the csv library, and the second by using the pandas library.

When you read CSV file using CSV Reader () function it returns the values in object?

Example 1: Read CSV files with csv. reader() is used to read the file, which returns an iterable reader object. The reader object is then iterated using a for loop to print the contents of each row.


2 Answers

The primary use of with statement is an exception-safe cleanup of an object used in the statement. with makes sure that files are closed, locks are released, contexts are restored, etc.

Does csv.reader have things to cleanup in case of exception?

I'd go with:

with open("myfile.csv") as f:     for row in csv.reader(f):         # process row 

You don't need to submit the patch to use csv.reader and with statement together.

import contextlib 

Help on function contextmanager in module contextlib:

contextmanager(func)     @contextmanager decorator. 

Typical usage:

    @contextmanager     def some_generator(<arguments>):         <setup>         try:             yield <value>         finally:             <cleanup> 

This makes this:

    with some_generator(<arguments>) as <variable>:         <body> 

equivalent to this:

    <setup>     try:         <variable> = <value>         <body>     finally:         <cleanup> 

Here's a concrete example how I've used it: curses_screen.

like image 76
jfs Avatar answered Sep 30 '22 19:09

jfs


Yes. The second way is correct.

As to why? Who ever knows. You're right, it's probably an easy change. It's not as high priority as other things.

You can easily make your own patch kit and submit it.

like image 29
S.Lott Avatar answered Sep 30 '22 20:09

S.Lott