Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why is 'rblabla' a valid csv file mode?

Tags:

python

csv

I have a csv file and a function to read it.

I can open it in many ways, most of these modes produce similar results.

def read(mode):
    with open("file.csv", mode) as inf:
        reader = csv.reader(inf)
        for row in reader:
            print row

read('r')       #prints \r\n characters
read('rb')      #prints \r\n characters
read('rU')      #prints \n characters but not \r characters
read('rblabla') #WAT.

I am wondering why the last example is allowed. It produces the same results as normal read mode.

Is there any reason why it works this way?

like image 426
e h Avatar asked Nov 24 '25 23:11

e h


1 Answers

The mode is not for the csv reader, but for the python default file handler. Python only enforces mode to begin with 'r', 'w' or 'a', after stripping U. This is documented here, and is for python 2.5 and later.

The mode is an attribute of the file handler, and may be used by other applications, hence it may contain more characters.

like image 80
Bach Avatar answered Nov 26 '25 13:11

Bach



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!