Codecademy Forums

Understanding the newline argument

How come the newline = ‘’ argument is not required in these answers even though they explicitly mention it multiple times in their examples?

One suspects it is because we are not writing to the file, but reading from it.

Right. Newline keyword argument is for reading. And what confused me was that the answer we’re supposed to give doesn’t require it. I suppose it’s because the file we’re reading does not contain any escape characters, so it’s safe to omit the Newline keyword. But I assume it’s considered a good practice to always put it there anyway, just in case, especially if we don’t know what the file looks like.

You and I are both guilty of assumptions… The blind leading the blind? It is possible there are no escape characters, as you reason, and that the delimiter says it all. I really don’t work enough with this sort of stuff so should keep my answers to myself (though I don’t mind being wrong, or only half right, so long as somebody eventually points it out).

1 Like

The following code reveals there are no escape characters, unless I’m misunderstanding how the .read() method works.

What does the output look like?

Author@ISBN@Title
Lauren Murray@978-0-12-995015-8@“Enviornment Call, Amount Later Page Country”
Micheal Jones@978-1-78110-100-1@Rate Security Full
Alexander Carr@978-0-315-25137-3@Still Response Size
Michael Williams@978-0-388-70665-7@Position Result Five
Kathleen Ferguson@978-1-75098-721-6@Country Week Receive And Sign
Sarah Dorsey@978-1-06-483628-6@Audience Truth Small
Mary Middleton@978-0-7419-8114-1@Travel: Special Offer Near Allow Goal
William Todd@978-1-4457-0480-7@Money Exactly Drop Teach
Joan Martin@978-0-657-61030-2@Theory Do Half Change
Gary Roman@978-1-5039-7539-2@Bill Serve Pull Industry South Job

Bur forget what I said. If you use .readlines() it shows the \n characters, so I still can’t figure it out.

open’s newline parameter is described in open’s docstring, which you can view with:

help(open)

After which you can decide whether or not you want to change the default value for that.

(Or read the docs for open at docs.python.org, specifically at https://docs.python.org/library/functions.html#open)

The documentation doesn’t specify the default value for the newline argument though. So it’s not clear to me what the code actually does when it is not passed the newline = [blank] argument.

Default values are shown in the signature
open(file, mode='r', buffering=-1, encoding=None, errors=None, newline=None, closefd=True, opener=None)

Reading the csv module’s documentation (which you arguably should to be able to use it at all) explains why the newline argument is included - the csv module’s documentation specifically mentions it

As for why the csv module wants it that way I probably don’t even want to know, csv files are a mess almost by definition (multiple standards, or perhaps more accurately multiple non-standards)
I suppose the reason for this module existing at all is that they are a mess.

1 Like

Ahhh sorry I’m blind. I was using ctrl+f on the word “default” hah Thank you very much, that clarifies it all. The Codecademy tutorial should explain it more clearly.

Well, fine, I do know why it wants it that way.
It is so that the csv module gets control over those newlines rather than allowing open’s text mode to do “the right thing” - presumably other tools flip out because they have borderline bugs in them and can’t deal with newlines correctly. If you’re not using such tools then you’re not going to notice the difference.

Uh. Well, I guess you might get weird things like '\r\r\n' if not giving the csv module control over newlines on a windows machine. The whole thing really just makes me want to run away.