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
rb' is for Read Binary mode.
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.
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.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With