Reading and Writing to Files with the Python Programming Language

2020-07-24 16:34:20 | #programming #python

In this tutorial, we're going to learn how to read and write to text files with Python.

Python is a great tool for quick and simple scripts because it has a shallow learning curve and much less bloat than alternatives like Java and C++.

How to Set Up a Python Project

Before you proceed, make sure you have Python installed by completing the prerequisites, above.

How to Create a Python Project with Windows 10 PowerShell 2.0+

cd ~
New-Item -ItemType "directory" -Path ".\python-file-parser"
cd python-file-parser
New-Item -ItemType "file" -Path . -Name "comma_delimited.txt"
New-Item -ItemType "file" -Path . -Name "tab_delimited.txt"
New-Item -ItemType "file" -Path . -Name "main.py"
virtualenv venv
.\venv\Scripts\activate

To verify that the virtual environment is active, make sure (venv) is in the PowerShell command prompt. For example, (venv) PS C:\Users\username\python-python-file-parser>

How to Create a Python Project with Linux Ubuntu 14.04+ or macOS

cd ~
mkdir python-file-parser
cd python-file-parser
virtualenv -p python3 venv
source venv/bin/activate
touch comma_delimited.txt
touch tab_delimited.txt
touch main.py

To verify that the virtual environment is active, make sure (venv) is in the terminal command prompt.

This will result in the following files and folder structure:

▾ python-file-parser/
  ▸ venv/
  comma_delimited.txt
  main.py
  tab_delimited.txt

Example Comma-Delimited File

Comma-delimited simply means that each column is separated by a comma. Our example defines the column headers in the first row, and devotes each subsequent row to a record's values. Copy and paste these contents into your own comma_delimited.txt file.

Filename: comma_delimited.txt

id,first_name,last_name,username
1,John,Doe,johndoe
2,Jane,Doe,janedoe

Example Tab-Delimited File

Here, tabs are used to separate each column. This is useful for when you have a column that contains commas or spaces and need to be read as one value. If the text file looks like it doesn't line up perfectly, don't be concerned. Copy and paste this as well.

Filename: tab_delimited.txt

id	first_name	last_name	username	address
1	John	Doe	johndoe	123 Elm St
2	Jane	Doe	janedoe	456 Elm St

Example Code for a Python File Reader

Filename: main.py

def read_file(path):
    with open(path, 'r') as data:
        return data.read()

if __name__ == '__main__':
    contents = read_file('comma_delimited.txt')
    print(contents)

Lines 1-3: defines a function that opens a file located at the path we pass into the function. The 'r' parameter tells the open method to open the file for reading. If we were to pass 'w', it would open the file for writing. With the file open, data.read() reads the entire contents, which gets returned.

With line 6, we invoke the function, store the returned value into the contents variable, and then print to the screen on line 7.

To execute this function, we run python3 main.py from the terminal, which displays the full contents of the text file:

id,first_name,last_name,username
  1,John,Doe,johndoe
  2,Jane,Doe,janedoe

Example Code for a Python Delimited File Reader

Filename: main.py

def read_delimited_file(path, delimiter):
    with open(path, 'r', newline='') as data:
        for row in data.readlines():
            cols = [col.rstrip('\n') for col in row.split(delimiter)]
            print(cols)


if __name__ == '__main__':
    read_delimited_file('comma_delimited.txt', ',')

This example gives us a function that accepts a path, as well as a delimiter. It also iterates over the rows and prints them to the screen. So that on line 9, we can invoke the function and print the column sets, be default. Or change line 5 to something like print(cols[0]) to only print the id column. cols is a list with each element representing a column.

You can see the column lists yourself by running python3 main.py

['id', 'first_name', 'last_name', 'username']
['1', 'John', 'Doe', 'johndoe']
['2', 'Jane', 'Doe', 'janedoe']

Example Code for a Converting a Delimited File to JSON with Python

Filename: main.py

def read_delimited_file(path, delimiter):
def read_delimited_file_to_dict(filename, delimiter):
    dict_data = {'data': []}
    with open('outfile.json', 'w') as outfile, open(filename, 'r', newline='') as data:
        rows = data.readlines()
        headers = rows[0].split(delimiter)
        for i, row in enumerate(rows[1:]):
            dict_object = {}
            for j, col in enumerate(row.split(delimiter)):
                dict_object[headers[j].strip('\n')] = col.strip('\n')
            dict_data['data'].append(dict_object)
        json.dump(dict_data, outfile)
        return dict_data


if __name__ == '__main__':
    dict_data = read_delimited_file_to_dict('comma_delimited.txt', ',')

    for d in dict_data['data']:
        print(d)
        print(d['id'], d['first_name'])

With this example, we have an even more intuitive way of accessing the columns and values. The function returns a list of dicts. By iterating over each list item, we can access the values by column name, i.e. d['first_name'].

To execute this code, run python3 main.py

{'id': '1', 'first_name': 'John', 'last_name': 'Doe', 'username': 'johndoe'}
1 John
{'id': '2', 'first_name': 'Jane', 'last_name': 'Doe', 'username': 'janedoe'}
2 Jane

Comments

You must log in to comment. Don't have an account? Sign up for free.

Subscribe to Our Newsletter

Would you like to receive free whitepapers and other IT news? Just leave your email address below. You may opt out at any time.



Tell Us About Your Project









Contact Us

Do you have a specific IT problem that needs solving or just have a general IT question? Use the contact form to get in touch with us and an IT professional will be with you, momentarily.

Hire Us

We offer web development, enterprise software development, QA & testing, google analytics, domains and hosting, databases, security, IT consulting, and other IT-related services.

Free IT Tutorials

Head over to our tutorials section to learn all about working with various IT solutions.

Contact