FAQ: Learn Python: Files - Reading a CSV File


#1

This community-built FAQ covers the “Reading a CSV File” exercise from the lesson “Learn Python: Files”.

Paths and Courses
This exercise can be found in the following Codecademy content:

Learn Python 3

FAQs on the exercise Reading a CSV File

There are currently no frequently asked questions associated with this exercise – that’s where you come in! You can contribute to this section by offering your own questions, answers, or clarifications on this exercise. Ask or answer a question by clicking reply (reply) below.

If you’ve had an “aha” moment about the concepts, formatting, syntax, or anything else with this exercise, consider sharing those insights! Teaching others and answering their questions is one of the best ways to learn and stay sharp.

Join the Discussion. Help a fellow learner on their journey.

Ask or answer a question about this exercise by clicking reply (reply) below!

Agree with a comment or answer? Like (like) to up-vote the contribution!

Need broader help or resources? Head here.

Looking for motivation to keep learning? Join our wider discussions.

Learn more about how to use this guide.

Found a bug? Report it!

Have a question about your account or billing? Reach out to our customer support team!

None of the above? Find out where to ask other questions here!


#2

Could somebody please explain to me in layman’s terms, what the newline = " " argument does? Documentation is written in a language which only takes me down a rabbit hole of words I don’t understand yet.


#3

Footnotes

[1] (1, 2) If newline='' is not specified, newlines embedded inside quoted fields will not be interpreted correctly, and on platforms that use \r\n linendings on write an extra \r will be added. It should always be safe to specify newline='' , since the csv module does its own (universal) newline handling.


#4

That’s the bit which I do not understand. What is meant by “on platforms that use \r\n linendings on write an extra \r will be added.”? Does it mean that the code will accidentally start writing at the beginning of the same line, because of incorrent interpretation? And why do we equate the word “newline” to an empty space as an argument? Am I simply missing some logical connection, or is the answer to that very technical, and I shouldn’t worry about it for now?


#5

Some platforms may insert their own newline escape characters (\n) that conflict with the csv module. It’s sufficient for now to just accept that there is good reason for the recommended implementation and in due course of time you will get more into the technical side of things. I would just pass on this question, for now.


#7

It would appear to overwrite the newline character so there are not two in a row when the module inserts its own. A space (or empty string) are the only substitutions that will not alter the data.


#8

I’ve found an interesting explanation in the documentation.

LINK

newline controls how universal newlines mode works (it only applies to text mode). It can be None , '' , '\n' , '\r' , and '\r\n' . It works as follows:

  • When reading input from the stream, if newline is None , universal newlines mode is enabled. Lines in the input can end in '\n' , '\r' , or '\r\n' , and these are translated into '\n' before being returned to the caller. If it is '' , universal newlines mode is enabled, but line endings are returned to the caller untranslated. If it has any of the other legal values, input lines are only terminated by the given string, and the line ending is returned to the caller untranslated.
  • When writing output to the stream, if newline is None , any '\n' characters written are translated to the system default line separator, os.linesep . If newline is '' or '\n' , no translation takes place. If newline is any of the other legal values, any '\n' characters written are translated to the given string.

#9

can anyone explain why in the solution for this exercise, they get rid of the step in which they initially created an empty list?