22

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
  • 132,704
  • 33
  • 254
  • 328
Lyra Orwell
  • 1,048
  • 4
  • 17
  • 46
  • 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 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

25

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

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

From the open() documentation:

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.

By the way, it's being removed in Python 3.11.

See also: Why is universal newlines mode deprecated in Python? - Software Engineering Stack Exchange

wjandrea
  • 28,235
  • 9
  • 60
  • 81