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.
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.
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.
It's the default behaviour now, so you can simply omit it:
with open("Keys.txt", "r") as csvfile:
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 anewline
argument. Setting it toNone
enables universal newlines. This is the accepted way to do it, rendering themode='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
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