473,698 Members | 2,451 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Store multiple dictionaries in a file

Hello,

I would like to store multiple dictionaries in a file, if possible one per
line. My code currently produces a new dictionary every iteration and
passes it on to another peace of code. In order to be able to re-run some
experiments at a later date I would like to store every dictionary in the
same file.
I looked at pickel, but that seems to require a whole file for each
dictionary.

It would be great if some one could tell me how to do that.

Thank you,
Phil

Jul 19 '05 #1
7 2559
Philipp H. Mohr wrote:
Hello,

I would like to store multiple dictionaries in a file, if possible one per
line.
Why "one per line" ?
My code currently produces a new dictionary every iteration and
passes it on to another peace of code.
May this code rest in piece <grin>
In order to be able to re-run some
experiments at a later date I would like to store every dictionary in the
same file.
I looked at pickel, but that seems to require a whole file for each
dictionary.

It would be great if some one could tell me how to do that.


A pretty simple solution could be to store all the dicts in another
container (list or dict, depending on how you need to retrieve'em, but
from what you explain I'd say a list) and then pickle this container.

My 2 cents...
--
bruno desthuilliers
python -c "print '@'.join(['.'.join([w[::-1] for w in p.split('.')]) for
p in 'o****@xiludom. gro'.split('@')])"
Jul 19 '05 #2

Thank you for you answer.
I would like to store multiple dictionaries in a file, if possible one per
line.
Why "one per line" ?


I agree with you that it sounds like nasty code :-) but there is a good
reason for doing it this way - I think. My code collects data (attributes)
of its current environment, e.g. date, time, location, etc.
These values are put into a dictionary and passed to another program which
processes the data. The dictionary (or vector of attributes) is the only
interface between both progs. The one which creates the dictionary can
forget about it after it has passed it on. This is where the storing comes
into action. In order to be able to re-run an experiment I want to store
the dictionaries in a file. Also the program might not run continuasly,
therefore if I write all of them to a file, on after the other, I would be
able to re-run the experiment much easier.

Hope this makes sense.

Thank you,
Phil

A pretty simple solution could be to store all the dicts in another
container (list or dict, depending on how you need to retrieve'em, but
from what you explain I'd say a list) and then pickle this container.

Jul 19 '05 #3
Philipp H. Mohr wrote:
I would like to store multiple dictionaries in a file, if possible one per
line. My code currently produces a new dictionary every iteration and
passes it on to another peace of code. In order to be able to re-run some
experiments at a later date I would like to store every dictionary in the
same file.
I looked at pickel, but that seems to require a whole file for each
dictionary.


If you're not worried about security, you could write the repr() of each
dict to the file and get the values back by using the eval() function.
repr() writes onto one line.

If you're storing types without repr() representations this will not work.

Jeremy

--
Jeremy Sanders
http://www.jeremysanders.net/
Jul 19 '05 #4
bruno modulix wrote:
Philipp H. Mohr wrote:

My code currently produces a new dictionary every iteration and
passes it on to another peace of code.

May this code rest in piece <grin>


Perhaps it's the piece of code that passeth all understanding?
Jul 19 '05 #5
bruno modulix wrote:
Philipp H. Mohr wrote:

My code currently produces a new dictionary every iteration and
passes it on to another peace of code.

May this code rest in piece <grin>


Perhaps it's the piece of code which passeth all understanding?
Jul 19 '05 #6
You might want to take a look at the shelve module.

-Larry

Philipp H. Mohr wrote:
Hello,

I would like to store multiple dictionaries in a file, if possible one per
line. My code currently produces a new dictionary every iteration and
passes it on to another peace of code. In order to be able to re-run some
experiments at a later date I would like to store every dictionary in the
same file.
I looked at pickel, but that seems to require a whole file for each
dictionary.

It would be great if some one could tell me how to do that.

Thank you,
Phil

Jul 19 '05 #7

Hello,

this is the solution I went for, as I am indeed not concernt about
security and the implementation is straight forward.

Thank you,
Phil

If you're not worried about security, you could write the repr() of each
dict to the file and get the values back by using the eval() function.
repr() writes onto one line.

If you're storing types without repr() representations this will not work.

Jeremy

Jul 19 '05 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
3394
by: Lars Behrens | last post by:
Hi there! For a web project I need a little expert help. I don't have written much code yet, just been fiddling around a bit, testing and planning. The web site will have a submission page for attendants of a congress. In a form the user will submit name, mailadress etc. and I plan to store the data in a list of dictionaries: People =
10
5837
by: Bulba! | last post by:
Hello everyone, I'm reading the rows from a CSV file. csv.DictReader puts those rows into dictionaries. The actual files contain old and new translations of software strings. The dictionary containing the row data looks like this: o={'TermID':'4', 'English':'System Administration', 'Polish':'Zarzadzanie systemem'}
5
4081
by: Livin | last post by:
I'm a noobie so please be easy on me. I have searched a ton and did not find anything I could understand. I'm using py2.3 I've been using Try/Except but this gets long with multiple dictionaries. I found some code on web pages and such but cannot get them to work. Any help is appreciated as I need to search multiple dictionaries for keys.
3
3393
by: manstey | last post by:
Hi, I am running a script that produces about 450,000 dictionaries. I tried putting them into a tuple and then pickling the tuple, but the tuple gets too big. Can I pickle dictionaries one after another into the same file and then read them out again? Cheers, Matthew
16
1629
by: IamIan | last post by:
Hello, I'm writing a simple FTP log parser that sums file sizes as it runs. I have a yearTotals dictionary with year keys and the monthTotals dictionary as its values. The monthTotals dictionary has month keys and file size values. The script works except the results are written for all years, rather than just one year. I'm thinking there's an error in the way I set my dictionaries up or reference them... import glob, traceback
7
2526
by: loial | last post by:
I need to store a list of variable names in a dictionary or list. I then later need to retrieve the names of the variables and get the values from the named variables. The named variables will already have been created and given a value. I hope thats clear!!! How can I do this?
6
3020
by: lysdexia | last post by:
I'm having great fun playing with Markov chains. I am making a dictionary of all the words in a given string, getting a count of how many appearances word1 makes in the string, getting a list of all the word2s that follow each appearance of word1 and a count of how many times word2 appears in the string as well. (I know I should probably be only counting how many times word2 actually follows word1, but as I said, I'm having great fun...
3
3551
by: shigehiro | last post by:
Hi all, Further to my 'list of dictionaries' question yesterday, I would to ask how to write data to csv file format using 'list of dictionaries' as a source: data to be written to .csv file: dictList = so the csv file would look something like: Michael,Kirk,224567 Linda,Matthew,123456
3
2341
by: M.-A. Lemburg | last post by:
On 2008-08-07 20:41, Laszlo Nagy wrote: 1 It also very fast at dumping/loading lists, tuples, dictionaries, floats, etc. -- Marc-Andre Lemburg eGenix.com
0
8676
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8608
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8867
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7732
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6522
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5860
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4370
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4619
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
2332
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.