Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the non deprecated version of open "U" mode

I am trying to read a textfile in python using:

with open("Keys.txt","rU") as csvfile:

however this produces a depreciation warning.

DeprecationWarning: 'U' mode is deprecated

What is the non deprecated version for this mode of access for a text/csv file.

like image 211
Lyra Orwell Avatar asked Jun 27 '19 12:06

Lyra Orwell


People also ask

What is u mode?

There is an additional mode character permitted, 'U' , which no longer has any effect, and is considered deprecated. It previously enabled universal newlines in text mode, which became the default behaviour in Python 3.0. Refer to the documentation of the newline parameter for further details.

What is universal newline mode?

A file object that has been opened in universal newline mode gets a new attribute “newlines” which reflects the newline convention used in the file. The value for this attribute is one of None (no newline read yet), "\r" , "\n" , "\r\n" or a tuple containing all the newline types seen.


1 Answers

It's the default behaviour now, so you can simply omit it:

with open("Keys.txt", "r") as csvfile:

More info

There is an additional mode character permitted, 'U', which no longer has any effect, and is considered deprecated. It previously enabled universal newlines in text mode, which became the default behaviour in Python 3.0. Refer to the documentation of the newline parameter for further details.

Source: open() - Python 3.7.4 documentation

The open() function in the Python 3 library has a newline argument. Setting it to None enables universal newlines. This is the accepted way to do it, rendering the mode='U' argument redundant.

Use newline=None to enable universal newlines mode (this is the default).

Source: Robert Harvey's answer on "Why is universal newlines mode deprecated in Python?" on Software Engineering

like image 172
wjandrea Avatar answered Sep 18 '22 16:09

wjandrea