How to Write an Array to a File in Python?

As a Python developer, I faced an issue while working on a project for a client in New York, I had a requirement to write an array to a file in Python. In this tutorial, I will cover various methods to achieve this with examples and screenshots of executed code.

Write Arrays to Files in Python

Let us look at all the important methods to achieve this task.

Check out How to Find the Index of the Maximum Value in an Array Using Python

1. Use Basic File Handling in Python

Python’s built-in file handling capabilities make it easy to write arrays to files. Here’s a simple example using a list of city names.

cities = ["New York", "Los Angeles", "Chicago", "Houston", "Phoenix"]

with open("cities.txt", "w") as file:
    for city in cities:
        file.write(city + "\n")

I executed the above code and added the screenshot below.

Write an Array to a File in Python

In this example, we open a file named cities.txt in write mode. We then iterate over the cities list and write each city to the file, followed by a newline character.

Read How to Convert Python Dict to Array

2. Write Numerical Data Using Python NumPy

Let’s consider a scenario where we have an array of average temperatures for different cities and want to save to a file. NumPy library offers efficient methods.

First, install NumPy if you haven’t already:

pip install numpy

Now, let’s write the array to a file:

import numpy as np

temperatures = np.array([55.3, 68.4, 77.1, 82.9, 75.4])

np.savetxt("temperatures.txt", temperatures, delimiter=",")

I executed the above code and added the screenshot below.

How to Write an Array to a File in Python

In this example, we use NumPy’s savetxt function to write the array to temperatures.txt, with values separated by commas.

Check out Is Python an Interpreted Language?

3. Using JSON for Complex Data Structures

For more complex data structures, such as lists of dictionaries, JSON is a great choice. Suppose we have a list of dictionaries representing different cities and their populations:

import json

cities = [
    {"name": "New York", "population": 8419000},
    {"name": "Los Angeles", "population": 3980000},
    {"name": "Chicago", "population": 2716000},
    {"name": "Houston", "population": 2328000},
    {"name": "Phoenix", "population": 1690000}
]

with open("cities.json", "w") as file:
    json.dump(cities, file, indent=4)

I executed the above code and added the screenshot below.

Write an Array to a File in Python Using JSON

Here, we use the json module to write the list of dictionaries to cities.json in a readable format, with an indentation of 4 spaces.

Read How to Update an Array in Python

4. Handle Multidimensional Python Arrays

Sometimes, you might need to write multidimensional arrays to a file. NumPy makes this task simple. Let’s consider an example where we have a 2D array representing the monthly average temperatures for several cities:

import numpy as np

temperatures = np.array([
    [30.2, 32.5, 45.3, 60.1, 72.5, 80.3, 85.6, 83.1, 75.4, 64.2, 50.5, 35.1],
    [40.1, 42.3, 55.2, 65.3, 75.4, 85.2, 90.5, 88.1, 80.3, 70.2, 55.1, 45.3]
])

np.savetxt("monthly_temperatures.txt", temperatures, delimiter=",")

In this example, we use savetxt again to write the 2D array to monthly_temperatures.txt, with values separated by commas.

Check out Python repeat array n times

5. NumPy Array to CSV with savetxt() in Python

Writing a NumPy Array to CSV with savetxt(). If your array is a NumPy array, the easiest way to save it to a file is by using NumPy’s built-in savetxt() function. This writes the array to a text file in CSV format.

import numpy as np

# Example array data
data = np.array([[1, 2, 3], [4, 5, 6]])

# Write array to CSV file 
np.savetxt('data.csv', data, delimiter=',')

Output:

1,2,3
4,5,6

Read Arrays from Files in Python

Knowing how to write arrays to files is not enough, you also need to know how to read files back. Let’s look at some examples.

1. Read from a Text File

If you have a simple text file with a list of cities, you can read it back into a list:

with open("cities.txt", "r") as file:
    cities = file.readlines()

cities = [city.strip() for city in cities]
print(cities)

Read How to Convert an Array to a Tuple in Python

2. Read Numerical Data with Python NumPy

To read numerical data saved with NumPy’s savetxt, use the loadtxt function:

import numpy as np

temperatures = np.loadtxt("temperatures.txt", delimiter=",")
print(temperatures)

Check out How to Print Duplicate Elements in Array in Python

3. Read JSON Files

For JSON files, use the json module to load the data back into a Python object:

import json

with open("cities.json", "r") as file:
    cities = json.load(file)

print(cities)

Read 3D Arrays in Python

4. Read Multidimensional Python Arrays

Reading multidimensional arrays saved with savetxt:

import numpy as np

temperatures = np.loadtxt("monthly_temperatures.txt", delimiter=",")
print(temperatures)

Check out How to Split a String into an Array in Python

Common Issues and Tips

1. Handle Large Files

When dealing with large files, it’s important to manage memory efficiently. Instead of reading the entire file once, consider reading it in chunks. This approach is useful when working with large datasets.

2. Choose the Right Format

Choose the file format based on your needs. For simple lists, a text file might be sufficient. For numerical data, NumPy’s binary format can be more efficient. For complex data structures, JSON is a versatile choice.

3. Error Handling

Always include error handling in your code to manage potential issues, such as file not found errors or permission issues.

try:
    with open("cities.txt", "r") as file:
        cities = file.readlines()
except FileNotFoundError:
    print("The file was not found.")
except PermissionError:
    print("You do not have permission to read this file.")

Check out Python program to print the smallest element in an array

Conclusion

In this tutorial, We covered lot many topics, I explained how to write an array to a file in Python using various methods like, basic file handling in Python, writing numerical data using Python NumPy, using JSON for complex data structures, handling multidimensional Python Arrays, NumPy Array to CSV with savetxt() I also discussed how to read arrays from files in Python by many ways like reading from a text file, reading numerical data with Python NumPy, reading JSON files, and how to read multidimensional Python arrays, I showed some common issues and tips for handling large files, choosing the right format, and error handling.

You may also like to read:

51 Python Programs

51 PYTHON PROGRAMS PDF FREE

Download a FREE PDF (112 Pages) Containing 51 Useful Python Programs.

pyython developer roadmap

Aspiring to be a Python developer?

Download a FREE PDF on how to become a Python developer.

Let’s be friends

Be the first to know about sales and special discounts.