In today’s tutorial we’ll learn to import Python lists into text files. We’ll cover three main use cases:
- Import list into a new file (could be txt, csv, json or other formats).
- 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')
else:
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 csv file.
office_emps = zip(offices, employees)
with open('my_file.csv', 'w') as my_file:
for (offices,employees) in office_emps:
my_file.write("{0},{1}\n".format(offices,employees))
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)
Python lists to csv with Pandas
Although the Python standard library provides useful methods to write list of objects to csv files, the Pandas 3rd party library provides some very elegant methods to accomplish this task. We first create a Pandas DataFrame from our data, and then export the data to a csv file located in our filesystem
Method 1: list of lists to DataFrame
# import the Pandas library into your development workspace
import pandas as pd
# define lists
offices = ['Atlanta', 'Boston', 'New York', 'Miami']
employees = [100,120,140,150]
#define list of lists.
my_list = [offices, employees]
#---------replace with method 2 as needed----------
# create DataFrame
hr_df = pd.DataFrame(my_list)
# Transpose the data and add column names
hr_df = hr_df.T
hr_df.columns = ['office', 'employees']
#export to csv format
hr_df.to_csv('hr.csv', index = False)
'#--------------------------------------------------
Method 2: using a dictionary to create the DataFrame
Here’s an alternative method, replace the lower part of the code in the section above with this snippet:
# Create dictionary
hr_dict = dict (office = my_list[0], employees = my_list[1])
# Create DataFrame from Dictionary
hr_df2 = pd.DataFrame(hr_dict, columns =['office', 'employees'] )
#export to csv format
hr_df2.to_csv('hr.csv', index = False)
Note: You can use the following snippet to write your lists without the header when exporting the Pandas DataFrame.
hr_df2.to_csv('hr.csv', index = False, header=False)
Result
Both methods outlined above will render a similar csv file: