How to write Python lists to txt and csv files?

In today’s tutorial we’ll learn to import Python lists into files. We’ll cover three main use cases:

  • Import list into a new file (could be txt, csv, json or others).
  • Append a list to an existing file.
  • Write multiple lists to a file
  • Write a list of dictionaries to a file
offices = ['Atlanta', 'Boston', 'New York', 'Miami']

Save list to a new text / csv file

We’ll start by creating the new file using the file open method, then loop through the list and write the list elements each in a different line.

with open('my_file.csv', 'w') as my_file:
my_file.write('Office branches:' + '\n')    
    for branch in offices:
        my_file.write(branch + '\n')
    print('File created')

Append Python list to text / csv file

In this example, we’ll first check whether our file exists in the operating system, and then append the list values to the file.

Here’s the code to use:

from pathlib import Path

my_file = Path('C:\Temp\my_file.xlsx')

# check if directory exists
if my_file.is_file():
   with open (my_file, 'a') as my_file:
    my_file.write('\n'+ 'Office branches:' + '\n')
    for o in offices:
       my_file.write(o + '\n') 
    print('File updated')
    print('File not available')

Write multiple lists to a file with Python

We would like now to import multiple lists into the file. One list has offices and the second has the corresponding number of employees.

offices = ['Atlanta', 'Boston', 'New York', 'Miami']
employees = [100,120,140,150]

We’ll now use the zip function to stitch the two lists, and then import them as needed into the a csv file.

office_emps = zip(offices, employees)

with open('my_file.csv', 'w') as my_file:
    for (offices,employees) in office_emps:
print('File created')

Here’s the output:

Export list of dictionaries to a file

We’ll use the json module to transfer the dictionary list.

dict1 = dict (Atlanta = 100, Boston = 120)
dict2 = dict(NewYork = 140, Miami=150)

my_dicts = [dict1, dict2]

import json
with open('my_dict.txt', 'w') as my_file:
    json.dump(my_dicts, my_file)