oldoc63 / learningDS

Learning DS with Codecademy and Books
0 stars 0 forks source link

Reading Different Types of CSV Files #361

Open oldoc63 opened 2 years ago

oldoc63 commented 2 years ago

It's true that CSV stands for comma-separated-values, but it's also true that other ways of separating values are valid CSV files these days.

We call all files with a list of different values a CSV file and then use different delimiters (like comma or tab) to indicate where the different values start and stop.

Let's say we had an address book. Since addresses usually use commas in then, we'll need to use a different delimiter for our information. Since none of our data has semicolons (;) in them, we can use those.

oldoc63 commented 2 years ago

Notice the \n character, this is the escape sequence for a new line. The possibility of a new line escaped by a \n character in our data is why we pass the newline='' keyword argument to the open() function. Also newline=None is valid. Including the newline parameter allows the csv module to handle the line endings itself - replicating the format as defined in your csv.

Also notice that many of the addresses have commas in them! We'll still be able to read it.

oldoc63 commented 2 years ago

When we call csv.DictReader we pass in the delimiter parameter, which is the string that's used to delineate separated fields in the CSV.