Python save content to file

Python Save Dictionary To File

In this lesson, you’ll learn how to save a dictionary to a file in Python. Also, we’ll see how to read the same dictionary from a file.

In this lesson, you’ll learn how to:

  • Use the pickle module to save the dictionary object to a file.
  • Save the dictionary to a text file.
  • Use the dump() method of a json module to write a dictionary in a json file.
  • Write the dictionary to a CSV file.

Table of contents

How to save a dictionary to file in Python

Dictionaries are ordered collections of unique values stored in (Key-Value) pairs. The below steps show how to use the pickle module to save the dictionary to a file.

    Import pickle module The pickle module is used for serializing and de-serializing a Python object structure.

Pickling” is the process whereby a Python object is converted into a byte stream, and “unpickling” is the inverse operation whereby a byte stream (from a binary file) is converted back into an original object.

Example: save a dictionary to file

Let’s see the below example of how you can use the pickle module to save a dictionary to a person_data.pkl file.

import pickle # create a dictionary using <> person = print('Person dictionary') print(person) # save dictionary to person_data.pkl file with open('person_data.pkl', 'wb') as fp: pickle.dump(person, fp) print('dictionary saved successfully to file')
Person dictionary dictionary saved successfully to file

Read Dictionary from a File

Now read the same dictionary from a file using a pickle module’s load() method.

import pickle # Read dictionary pkl file with open('person_data.pkl', 'rb') as fp: person = pickle.load(fp) print('Person dictionary') print(person)

Save a dictionary to a text file using the json module

We can use the Python json module to write dictionary objects as text data into the file. This module provides methods to encode and decode data in JSON and text formats.

Читайте также:  Что такое линтер css

We will use the following two methods of a json module.

  • The dump() method is used to write Python objects as JSON formatted data into a file.
  • Using the load() method, we can read JSON data from text, JSON, or a binary file to a dictionary object.

Let’s see the below example of how you can use the json module to save a dictionary to a text file.

import json # assume you have the following dictionary person = print('Person dictionary') print(person) print("Started writing dictionary to a file") with open("person.txt", "w") as fp: json.dump(person, fp) # encode dict into JSON print("Done writing dict into .txt file")
Person dictionary Started writing dictionary to a file Done writing dict into .txt file

Person text file

Note: You can also use the dump() method to write a dictionary in a json file. Only you need to change the file extension to json while writing it.

Read a dictionary from a text file.

Now, let’s see how to read the same dictionary from the file using the load() function.

import json # Open the file for reading with open("person.txt", "r") as fp: # Load the dictionary from the file person_dict = json.load(fp) # Print the contents of the dictionary print(person_dict)

Save the dictionary to a CSV file

The Python csv library provides functionality to read from and write to CSV files.

  • Use the csv.DictReader() method to read CSV files into a dictionary.
  • Use the csv.DictWriter() method to write a dictionary to a CSV file.

Example: Save the dictionary to a CSV file.

import csv # Dictionary to be saved person = print('Person dictionary') print(person) # Open a csv file for writing with open("person.csv", "w", newline="") as fp: # Create a writer object writer = csv.DictWriter(fp, fieldnames=person.keys()) # Write the header row writer.writeheader() # Write the data rows writer.writerow(person) print('Done writing dict to a csv file')
Person dictionary Done writing dict to a csv file

Person csv file

Example: Read a dictionary from a csv file

import csv # Open the csv file for reading with open("person.csv", "r") as infile: # Create a reader object reader = csv.DictReader(infile) # Iterate through the rows for row in reader: print(row)
OrderedDict([('name', 'Jessa'), ('country', 'USA'), ('telephone', '1178')])

Note: This will read the contents of the person.csv file and create a dictionary for each row in the file. You can then iterate through the rows and access the values in the dictionary using the column names as keys.

Did you find this page helpful? Let others know about it. Sharing helps me continue to create free Python resources.

About Vishal

I’m Vishal Hule, Founder of PYnative.com. I am a Python developer, and I love to write articles to help students, developers, and learners. Follow me on Twitter

Python Exercises and Quizzes

Free coding exercises and quizzes cover Python basics, data structure, data analytics, and more.

  • 15+ Topic-specific Exercises and Quizzes
  • Each Exercise contains 10 questions
  • Each Quiz contains 12-15 MCQ

Источник

Saving Text, JSON, and CSV to a File in Python

Saving data to a file is one of the most common programming tasks you may come across in your developer life.

Generally, programs take some input and produce some output. There are numerous cases in which we’d want to persist these results. We may find ourselves saving data to a file for later processing — from webpages we browse, simple dumps of tabular data we use for reports, machine learning and training or logging during the application runtime — we rely on applications writing to files rather than doing it manually.

Python allows us to save files of various types without having to use third-party libraries. In this article, we’ll dive into saving the most common file formats in Python.

Opening and Closing a File

Opening a File

The contents of a file can be accessed when it’s opened, and it’s no longer available for reading and writing after it’s been closed.

Opening a file is simple in Python:

my_data_file = open('data.txt', 'w') 

When opening a file you’ll need the filename — a string that could be a relative or absolute path. The second argument is the mode, this determines the actions you can do with the open file.

Here are some of the commonly used ones:

  • r — (default mode) open the file for reading
  • w — open the file for writing, overwriting the content if the file already exists with data
  • x — creates a new file, failing if it exists
  • a — open the file for writing, appending new data at the end of the file’s contents if it already exists
  • b — write binary data to files instead of the default text data
  • + — allow reading and writing to a mode

Let’s say you wanted to write to a file and then read it after, your mode should be ‘w+’. If you wanted to write and then read from a file, without deleting the previous contents then you’ll use ‘a+’.

Closing a File

Closing a file is even easier in Python:

You simply need to call the close method on the file object. It’s important to close the file after you are finished using it, and there are many good reasons to do so:

  • Open files take up space in RAM
  • Lowers chance of data corruption as it’s no longer accessible
  • There’s a limit of files your OS can have open

For small scripts, these aren’t pressing concerns, and some Python implementations will actually automatically close files for you, but for large programs don’t leave closing your files to chance and make sure to free up the used resources.

Using the «with» Keyword

Closing a file can be easily forgotten, we’re human after all. Lucky for us, Python has a mechanism to use a file and automatically close it when we’re done.

To do this, we simply need to use the with keyword:

with open('data.txt', 'w') as my_data_file: # TODO: write data to the file # After leaving the above block of code, the file is closed 

The file will be open for all the code that’s indented after using the with keyword, marked as the # TODO comment. Once that block of code is complete, the file will be automatically closed.

This is the recommended way to open and write to a file as you don’t have to manually close it to free up resources and it offers a failsafe mechanism to keep your mind on the more important aspects of programming.

Saving a Text File

Now that we know the best way to access a file, let’s get straight into writing data.

Fortunately, Python makes this straightforward as well:

with open('do_re_mi.txt', 'w') as f: f.write('Doe, a deer, a female deer\n') f.write('Ray, a drop of golden sun\n') 

The write() function takes a string and puts that content into the file stream. Although we don’t store it, the write() function returns the number of characters it just entered i.e. the length of the input string.

Note: Notice the inclusion of the newline character, \n . It’s used to write to a next line in the file, otherwise, all the text would be added as a single line.

Saving Multiple Lines at Once

With the write() function we can take one string and put it into a file. What if we wanted to write multiple lines at once?

We can use the writelines() function to put data in a sequence (like a list or tuple) and into a file:

Free eBook: Git Essentials

Check out our hands-on, practical guide to learning Git, with best-practices, industry-accepted standards, and included cheat sheet. Stop Googling Git commands and actually learn it!

with open('browsers.txt', 'w') as f: web_browsers = ['Firefox\n', 'Chrome\n', 'Edge\n'] f.writelines(web_browsers) 

As before, if we want the data to appear in new lines we include the newline character at the end of each string.

If you’d like to skip the step of manually entering the newline character after each item in the list, it’s easy to automate it:

with open('browsers.txt', 'w') as f: web_browsers = ['Firefox\n', 'Chrome\n', 'Edge\n'] f.writelines("%s\n" % line for line in web_browsers) 

Note: The input for writelines() must be a flat sequence of strings or bytes — no numbers, objects or nested sequences like a list within a list are allowed.

If you’re interested in reading more about lists and tuples, we already have an article dedicated to them — Lists vs Tuples in Python.

Saving a CSV File

CSV (Comma Separated Values) files are commonly used for storing tabular data. Because of its popularity, Python has some built-in methods to make writing files of that type easier:

import csv weekdays = ['Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday'] sales = ['10', '8', '19', '12', '25'] with open('sales.csv', 'w') as csv_file: csv_writer = csv.writer(csv_file, delimiter=',') csv_writer.writerow(weekdays) csv_writer.writerow(sales) 

We first need to import the csv library to get their helper functions. We open the file as we’re accustomed to but instead of writing content on the csv_file object, we create a new object called csv_writer .

This object provides us with the writerow() method which allows us to put all the row’s data in the file in one go.

If you’d like to learn more about using CSV files in Python in more detail, you can read more here: Reading and Writing CSV Files in Python.

Saving a JSON File

JSON is another popular format for storing data, and just like with CSVs, Python has made it dead simple to write your dictionary data into JSON files:

import json my_details = < 'name': 'John Doe', 'age': 29 > with open('personal.json', 'w') as json_file: json.dump(my_details, json_file) 

We do need to import the json library and open the file. To actually write the data to the file, we just call the dump() function, giving it our data dictionary and the file object.

If you’d like to know more about using JSON files in Python, you can more from this article: Reading and Writing JSON to a File in Python.

Conclusion

Saving files can come in handy in many kinds of programs we write. To write a file in Python, we first need to open the file and make sure we close it later.

It’s best to use the with keyword so files are automatically closed when we’re done writing to them.

We can use the write() method to put the contents of a string into a file or use writelines() if we have a sequence of text to put into the file.

For CSV and JSON data, we can use special functions that Python provides to write data to a file once the file is open.

Источник

Оцените статью