[Tutor] CSV Module

Mats Wichmann mats at wichmann.us
Sun Sep 27 11:12:32 EDT 2020


On 9/25/20 11:18 PM, Manprit Singh wrote:
> Dear Sir,
> 
> Consider a tuple (more precisely a nested tuple)as given below :
> tup = (("1", "Andy", "Science",),
>            ("2", "Robin", "Arts",), )
> 
> First of all i just need to know , if i maintain a habit to include a comma
> even after the last element of tuple and list . As you can see I have
> placed a comma just after "Science"  and "Arts"  as well as after the two
> tuples ("1", "Andy", "Science",) and
> ("2", "Robin", "Arts",) . Is it a good habit ? I read somewhere that it is
> better to write lists and tuples in this way . Need your comments.

One reason people suggest the trailing comma is it shows less change in
a diff if you later add elements. That consideration only matters on
multi-line definitions.  That is, if you add an element to your internal
tuple so:

("1", "Andy", "Science",),

becomes

("1", "Andy", "Science", "Freshman".),

well - the line changed anyway, so you didn't save anything by the
trailing comma...

but if you add lines, and lay it out like this:

tup = (("1", "Andy", "Science",),
           ("2", "Robin", "Arts",),
)

Now adding a new entry is just adding a line, and the diff won't also
show the "2" tuple as having changed, because you needed to add a
trailing comma to it becasuse it's no longer the last element.

Does that make sense?

> Another  Question is now if i have to read the same csv, What about the
> below written code. Need your humble comments
> 
> with open('some.csv', newline='') as f:
>     reader = csv.reader(f)                      # Reader object
>     for row in reader:
>         print(row)                                     # Prints each row
> 
> Coming to the main point now :
> The code written just above closes the file at the point when  all rows are
> printed, and once the file is closed you can not again access the same file
> without repeating the same process . Now if i have to access this csv file
> lots of time in a single program , Repeating the same above written process
> again and again is the only way ?  Can i assign this CSV file to a variable
> so that i can use this csv file number of times in a programme ?Need your
> humble comments in this regard

The use of a context manager (with statement) is nice because it handles
cleanup for you after the context is complete, something which it turns
out is easy to forget.  If your context for manipulating the file is
bigger than that, and not cleanly contained in one smallish code block,
then don't use a context manager - just remember to clean up after yourself.

Yes, of course you can assign the open file object to a name and
continue to use it.

f = open('some.csv, newline='')

But... once you've read all the data from the file the way you're
showing, you're "off the end" and any future reads will return empty.
So if you want to continue using you're going to need to reset the file
pointer ... for example the seek method.  You'll perhaps also want to
think about what mode you open the file in (if you don't supply the
mode, it defaults to 'r' - read only).

I think there are people who feel if you've got access to a file
scattered all around your code instead of in a neat self-contained block
suitable for a context manager, you may need to consider if your code is
optimally organized... just something else to think about.



More information about the Tutor mailing list