Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Whats the difference between 'rb' and 'rU' in the open() function for csv

I was wondering what the difference between:

with codecs.open('xxxx.csv', 'rU') as h:

and

with codecs.open('xxxx.csv', 'rb') as h:

I think I remember someone saying you should use 'rb' and not 'rU' when I started my project with reading .csv files, but I cant seem to find it again.

Anybody who would like to explain this? Thanks

like image 394
The Dude Avatar asked Jun 29 '16 07:06

The Dude


People also ask

What is RB in csv?

rb' is for Read Binary mode.

What is RB in open?

The open() function opens a file in text format by default. To open a file in binary format, add 'b' to the mode parameter. Hence the "rb" mode opens the file in binary format for reading, while the "wb" mode opens the file in binary format for writing. Unlike text files, binary files are not human-readable.

What does RB mean in file?

rb : Opens the file as read-only in binary format and starts reading from the beginning of the file. While binary format can be used for different purposes, it is usually used when dealing with things like images, videos, etc. r+ : Opens a file for reading and writing, placing the pointer at the beginning of the file.

What is the difference between reader () and DictReader () function?

Reader() allows you to access CSV data using indexes and is ideal for simple CSV files. csv. DictReader() on the other hand is friendlier and easy to use, especially when working with large CSV files.


1 Answers

As the documentation states, U (Universal Newlines) mode is deprecated; you should not use it anymore. Instead use the newline= keyword argument.

The csv documentation states that it prefers that parameter to be '', so open won't do any interpretation of newlines and leave that up to the csv module.

Since you likely want to decode your CSV into text (not bytes), it makes no real sense to open them in b (binary) mode.

Bottomline: the usual way to parse CSV files is:

with open('eggs.csv', newline='') as csvfile:
    spamreader = csv.reader(csvfile)
    for row in spamreader:
        ...

This means you're using the implicit open mode rt, for reading in text mode. Unless you have very special needs, this is probably what you want. And the above sample code is taken straight from the documentation.

like image 117
deceze Avatar answered Oct 30 '22 01:10

deceze