nuwc_nps_banner.png

Lesson 1.7: Working with data files

In this lesson, we will discuss the basics of working with data:

Parsing Strings to Lists

This is what is happening...

Successive delimiters are treated as one...

The resulting listOfWords is the same in these three cases:

You can choose any delimiter you'd like...

File IO: File Types

There are two types of files:

In this course, we are going to restrict attention to text files only.

Simple Output Files: Redirecting Program Output

python myscript.py > outfile.txt

This is called "redirecting file output" and it has nothing to do with Python per se. When we are doing this, we are taking advantage of a feature of the operating system. Nonetheless, it's an easy and powerful way of creating an output file.

python myscript.py >> outfile.txt

Each time the program myscript.py is called in this manner, its output is added to the end of the existing file outfile.txt.

Writing Directly to a File

We also have the ability for our Python program to write "directly" to a file. In order to do this, we must include Python commands to execute each of the following steps:

Note that the file will be written to your current "working" directory.

Here is a simple example:

Having executed the code block above, we should now see a file named sample1.txt in the same directory as this notebook.

Reading Data Directly from a File

We can also create Python programs that read the contents of a file.

In order to do this, we must include Python commands to execute each of the following steps:

Note that the file must already exist in your current "working" directory.

For example, we can use Python to read the file we just wrote.

Here are some simple file reading scripts:

In the block of code above, the read() function read the entire file, which is typically not very useful in practice. Instead, we typically read a file line-by-line.

What a pain. It would be much better to use a loop. As with all loops, there is more than one way to do this. Here's a common one:

Comma Separated Value (CSV) data

The csv module

Reading and writing comma-separated value (CSV) data is so common that there is a Python module to make it easier. Check out https://docs.python.org/3/library/csv.html.

Key features of this module: