254

I have problem with changing a dict value and saving the dict to a text file (the format must be same), I only want to change the member_phone field.

My text file is the following format:

memberID:member_name:member_email:member_phone

and I split the text file with:

mdict={}
for line in file:
    x=line.split(':')
    a=x[0]
    b=x[1]
    c=x[2]
    d=x[3]
    e=b+':'+c+':'+d

    mdict[a]=e

When I try change the member_phone stored in d, the value has changed not flow by the key,

def change(mdict,b,c,d,e):
    a=input('ID')
    if a in mdict:
        d= str(input('phone'))
        mdict[a]=b+':'+c+':'+d
    else:
        print('not')

and how to save the dict to a text file with same format?

martineau
  • 119,623
  • 25
  • 170
  • 301
XueYuan Wang
  • 2,541
  • 2
  • 12
  • 3

12 Answers12

428

Python has the pickle module just for this kind of thing.

These functions are all that you need for saving and loading almost any object:

import pickle 

with open('saved_dictionary.pkl', 'wb') as f:
    pickle.dump(dictionary, f)
        
with open('saved_dictionary.pkl', 'rb') as f:
    loaded_dict = pickle.load(f)

In order to save collections of Python there is the shelve module.

Hadij
  • 3,661
  • 5
  • 26
  • 48
Zah
  • 6,394
  • 1
  • 22
  • 34
  • 2
    `save_obj` seems to require that the file `obj/'+ name + '.pkl` already exist. I created a dictionary named `Q`, populated it, and made the call `save_obj(Q, "Qtable")` I got an error: `FileNotFoundError: [Errno 2] No such file or directory: 'obj/Qtable.pkl'` How does one create the file in the first place before writing to it? – Toothpick Anemone Oct 24 '17 at 19:58
  • 1
    @ToothpickAnemone use `wb+` to create file, i.e.: `with open('obj/'+ name + '.pkl', 'wb+')` – andrey.s Nov 04 '17 at 04:39
  • 3
    @andrey.s: I don't think what you say will make any difference because it doesn't address the problem. – martineau Dec 18 '17 at 20:58
  • 5
    @Toothpick Anemone: Adding a `+` to the mode will have no affect on your problem (andrey.s is incorrect). Your problem sounds like it's because of one or two things. For the `save_obj()` in this answer to work, a subdirectory named `"obj"` must **already exist** because `open()` won't create one automatically. Second the first argument to `save_obj()` is the Python object to be saved, not the name of the subdirectory (although it's not entirely clear what you meant by the `Q` in the example `save_obj(Q, "Qtable")` call). You can create a directory if it doesn't already exits with `os.mkdir()`. – martineau Dec 18 '17 at 20:58
  • is there a reason the 'obj/' string is even added here? In general the default behavior for saving anything is to save in the current directory or you give it the full directory to save there. I just removed it and it is working as expected! – Phillip Maire Oct 07 '21 at 21:09
239

Pickle is probably the best option, but in case anyone wonders how to save and load a dictionary to a file using NumPy:

import numpy as np

# Save
dictionary = {'hello':'world'}
np.save('my_file.npy', dictionary) 

# Load
read_dictionary = np.load('my_file.npy',allow_pickle='TRUE').item()
print(read_dictionary['hello']) # displays "world"

FYI: NPY file viewer

10xAI
  • 154
  • 1
  • 8
Franck Dernoncourt
  • 77,520
  • 72
  • 342
  • 501
  • 7
    what's the .item() for? – Gulzar May 22 '18 at 14:00
  • Why is this answer less upvoted than @Zah 's "pickle" answer? More space-complex? – Nathan majicvr.com Jun 09 '18 at 18:38
  • 3
    @frank possibility because pickle is part of python's standard library where numpy is an independent project. – Maxim Veksler Jul 03 '18 at 11:27
  • 4
    @Gulzar from what I looked up, np.load returns an ndarray (doing a `type(read_dictionary)` reveals so) and .item() basically converts that element to a python scalar object which is a dictionary as stated [here](https://docs.scipy.org/doc/numpy/reference/generated/numpy.ndarray.item.html#numpy.ndarray.item) – otaku Sep 07 '18 at 00:42
  • I got an error only trying to import NumPy: "import numpy as np ImportError: No module named numpy" – Shai Alon Nov 20 '18 at 14:53
  • 2
    @Shai have you installed the numpy package...? – Eben Dec 01 '18 at 05:26
  • Why is `allow_pickle`'s value a string, `'TRUE'`? The [docs](https://docs.scipy.org/doc/numpy/reference/generated/numpy.load.html) say it's a boolean. – jds Mar 05 '20 at 16:34
  • If I'm not mistaken, after saving in .npy format, you can't view data with a simple text editor. That's a big drawback of this method compared to saving as json. – secluded Jul 07 '20 at 11:42
  • From this code I got the error "ValueError: can only convert an array of size 1 to a Python scalar". It was sorted with changing the code to: read_dictionary = np.load('my_file.npy',allow_pickle='TRUE').tolist() – chilifan Jun 20 '23 at 08:36
90

We can also use the json module in the case when dictionaries or some other data can be easily mapped to JSON format.

import json

# Serialize data into file:
json.dump( data, open( "file_name.json", 'w' ) )

# Read data from file:
data = json.load( open( "file_name.json" ) )

This solution brings many benefits, eg works for Python 2.x and Python 3.x in an unchanged form and in addition, data saved in JSON format can be easily transferred between many different platforms or programs. This data are also human-readable.

simhumileco
  • 31,877
  • 16
  • 137
  • 115
  • 14
    Good approach, but I don't think it is safe to use `open` directly instead of a context created by `with`. Is there a gurantee, the file handle will be closed if `json.dump` fails? – Dr_Zaszuś May 26 '20 at 13:58
  • 8
    For a nice looking, human readable .json, do the following: `with open('file_name.json', 'w', encoding='utf-8') as f: json.dump(data, f, ensure_ascii=False, indent=4)` – MiKK Jan 04 '22 at 20:08
30

As Pickle has some security concerns and is slow (source), I would go for JSON, as it is fast, built-in, human-readable, and interchangeable:

import json
data = {'another_dict': {'a': 0, 'b': 1}, 'a_list': [0, 1, 2, 3]}
# e.g. file = './data.json' 
with open(file, 'w') as f: 
    json.dump(data, f)

Reading is similar easy:

with open(file, 'r') as f:
    data = json.load(f)

This is similar to this answer, but implements the file handling correctly.

If the performance improvement is still not enough, I highly recommend orjson, a fast, correct JSON library for Python built upon Rust.

For those focussing on efficient storage over run-time performance, I recommend using compression, for example bz2:

import json 
import bz2

byte_data = json.dumps(data)
compressed_byte_data = bz2.compress(byte_data)
with open(file_path, 'wb') as file_handle:
    file_handle.write(compressed_byte_data)
Michael Dorner
  • 17,587
  • 13
  • 87
  • 117
24

Save and load dict to file:

def save_dict_to_file(dic):
    f = open('dict.txt','w')
    f.write(str(dic))
    f.close()

def load_dict_from_file():
    f = open('dict.txt','r')
    data=f.read()
    f.close()
    return eval(data)
junglejet
  • 257
  • 2
  • 2
21

I'm not sure what your first question is, but if you want to save a dictionary to file you should use the json library. Look up the documentation of the loads and puts functions.

roschach
  • 8,390
  • 14
  • 74
  • 124
John
  • 723
  • 1
  • 5
  • 12
13

I would suggest saving your data using the JSON format instead of pickle format as JSON's files are human-readable which makes your debugging easier since your data is small. JSON files are also used by other programs to read and write data. You can read more about it here

You'll need to install the JSON module, you can do so with pip:

pip install json


# To save the dictionary into a file:
json.dump( data, open( "myfile.json", 'w' ) )

This creates a json file with the name myfile.

# To read data from file:
data = json.load( open( "myfile.json" ) )

This reads and stores the myfile.json data in a data object.

blueseal
  • 2,726
  • 6
  • 26
  • 41
Kavin Sabharwal
  • 139
  • 1
  • 3
3

For a dictionary of strings such as the one you're dealing with, it could be done using only Python's built-in text processing capabilities.

(Note this wouldn't work if the values are something else.)

with open('members.txt') as file:
    mdict={}
    for line in file:
        a, b, c, d = line.strip().split(':')
        mdict[a] = b + ':' + c + ':' + d

a = input('ID: ')
if a not in mdict:
    print('ID {} not found'.format(a))
else:
    b, c, d = mdict[a].split(':')
    d = input('phone: ')
    mdict[a] = b + ':' + c + ':' + d  # update entry
    with open('members.txt', 'w') as file:  # rewrite file
        for id, values in mdict.items():
            file.write(':'.join([id] + values.split(':')) + '\n')
martineau
  • 119,623
  • 25
  • 170
  • 301
  • This doesn't work on dictionaries: file.write(':'.join([id] + values.split(':')) + '\n') AttributeError: 'dict' object has no attribute 'split' – Shai Alon Nov 20 '18 at 14:57
  • @ShaiAlon: Not with all of them, no. It definitely **does** work with those whose values are strings (that have a `split()` method)—which is the topic of this question. It sounds like you're trying to use it on a dictionary of dictionaries, so no it wouldn't work with those. I also don't appreciate down-votes from folks because they don't really understand the question (and thus the context of the answers being provided). I'll update my answer to make when using it would be appropriate, so please undo your down-vote. – martineau Nov 20 '18 at 17:21
3

I like using the pretty print module to store the dict in a very user-friendly readable form:

import pprint

def store_dict(fname, dic):
    with open(fname, "w") as f:
        f.write(pprint.pformat(dic, indent=4, sort_dicts=False))
        # note some of the defaults are: indent=1, sort_dicts=True

Then, when recovering, read in the text file and eval() it to turn the string back into a dict:

def load_file(fname):
    try:
        with open(fname, "r") as f:
            dic = eval(f.read())
    except:
        dic = {}
    return dic
questionto42
  • 7,175
  • 4
  • 57
  • 90
GaryMBloom
  • 5,350
  • 1
  • 24
  • 32
2

Unless you really want to keep the dictionary, I think the best solution is to use the csv Python module to read the file. Then, you get rows of data and you can change member_phone or whatever you want ; finally, you can use the csv module again to save the file in the same format as you opened it.

Code for reading:

import csv

with open("my_input_file.txt", "r") as f:
   reader = csv.reader(f, delimiter=":")
   lines = list(reader)

Code for writing:

with open("my_output_file.txt", "w") as f:
   writer = csv.writer(f, delimiter=":")
   writer.writerows(lines)

Of course, you need to adapt your change() function:

def change(lines):
    a = input('ID')
    for line in lines:
      if line[0] == a:
        d=str(input("phone"))
        line[3]=d
        break
    else:
      print "not"
mguijarr
  • 7,641
  • 6
  • 45
  • 72
1

I haven't timed it but I bet h5 is faster than pickle; the filesize with compression is almost certainly smaller.

import deepdish as dd
dd.io.save(filename, {'dict1': dict1, 'dict2': dict2}, compression=('blosc', 9))
wordsforthewise
  • 13,746
  • 5
  • 87
  • 117
1
file_name = open("data.json", "w")
json.dump(test_response, file_name)
file_name.close()

or use context manager, which is better:

with open("data.json", "w") as file_name:
    json.dump(test_response, file_name)
Mikhail Stepanov
  • 3,680
  • 3
  • 23
  • 24