19

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.

Jonathon Reinhart
  • 124,861
  • 31
  • 240
  • 314
Lyra Orwell
  • 908
  • 2
  • 12
  • 35
  • In Python >= 3, use newline instead, the default is newline=None which is like newline='' except it also translates the newlines to \n. I'm unsure which of these is equivalent to mode='U' Referenced here https://stackoverflow.com/questions/6726953/open-the-file-in-universal-newline-mode-using-the-csv-django-module – Kyle J Jun 27 '19 at 13:01
  • @KyleJ: `U` behavior is part of the default behavior. You just remove it. As you say, `csv` needs `newline=''`, but that has nothing to do with `U` (which can just be removed). – ShadowRanger Jun 27 '19 at 13:06

1 Answers1

20

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

wjandrea
  • 23,210
  • 7
  • 49
  • 68