Getting started with importing data in Python

Hugo Baun Andersen covers importing data from text files, CSVs, Excel, databases, and more. He shows how to read files with Python's open() function and introduces the with statement as a cleaner way to handle file closure automatically.
Chapters
Welcome to the first course on importing data in Python. My name is Hugo Baun Andersen and I'm a Data Scientist at DataCamp. In this course you'll learn how to import data from a large variety of sources, for example flat files such as txts and CSVs files native to other software such as Excel spreadsheets, Stata, SAS and MATLAB files. Relational databases such as SQLite and MySQL we'll cover all of these topics in this course. First off, we're going to learn how to import basic text files, which we can broadly classify into two types of files. Those containing plain text, such as the opening of Mark Twain's novel the Adventures of Huckleberry Finn, which you can see here Those containing records, I.e. table data such as Titanic CSV, in which each row is a unique passenger on board and each column is a characteristic or feature, such as Sex cabin and survived or not. The latter is known as a flat file, and we'll come back to these in a minute. In this section we'll figure out how to read lines from a plain text file. So let's do it. To check out any plaintext file, you can use Python's basic open function to open a connection to the file. To do so, you assign the file name to a variable as a string, pass the filename to the function open, and also pass it the argument mode R, which makes sure that we can only read it. We wouldn't want to accidentally write to it, would we? Assign the text from the file to a variable text by applying the method read to the connection to the file. After you do this, make sure that you close the connection to the file using the command file close. It's always best practice to clean while cooking. You can then print the file to the console and check it out using the command print text. A brief side note, if you wanted to open a file in order to write to it, you would pass it the argument mode W. We won't use that in this course, as this is a course on importing data, but it is good to know you can avoid having to close the connection to the file by using a with statement. This allows you to create a context in which you can execute commands with the file open. Once out of this clause or context, the file is no longer open, and for this reason with is called a context manager. What you're doing here is called binding a variable in the contextmanager construct. While still within this construct, the variable file will be bound to open filename R. It is best practice to use the with statement, as you never have to concern yourself with closing the file again. In the following interactive coding sessions, you'll figure out how to print files to the console. You'll also learn to print specific lines, which can be very useful for large files. Then we'll be back to discuss flat files, and then I'll show you how to use the Python package numpy to make our job of importing flat files and numerical data a far easier beast to tame. Enjoy.
More from Richie

#359 My Best Friend is AI with Valerie Tiberius, Professor of Philosophy at University of Minnesota

How AI Agents Will Work While You Sleep | Ruslan Salakhutdinov, Professor at Carnegie Mellon

#358 How AI Agents Will Work While You Sleep | Ruslan Salakhutdinov, Professor at Carnegie Mellon







