0

i'm rather new to python and coding in general. I'm writing my own chat statistics bot for russian social net (vk. com).

My question is can i store a dictionary in a file and work with it?

For example:

Userlist=open('userlist.txt', '+')
If lastmessage['uid'] not in Userlist.read():
     Userlist.read()[lastmessage.'uid']=1
Userlist.close()

Or do i have to use some side modules like JSON?

Thank you

Warpony
  • 31
  • 5
  • So, you are looking to store a dictionary in a file, and then read it back in to your code to use later? – idjaw Feb 27 '16 at 22:49
  • Use `json` is very simple. `obj = json.load(f)` to read, and `json.dump(obj, f)` to save – Xiongbing Jin Feb 27 '16 at 22:50
  • 2
    You can `pickle` any python structure. – roadrunner66 Feb 27 '16 at 22:50
  • It seems what you're really looking for is a database. – 5gon12eder Feb 27 '16 at 22:51
  • @5gon12eder i only need to store a dictionary as virgin as it is possible. – Warpony Feb 27 '16 at 22:56
  • 1
    @idjaw, in the while true cycle i want to check, if a user's id is in 'userlist' dictionary (as a key) and if not, add it to this dictionary with value 1. Then i want to rewrite the file with a new dictionary. the file is opened as soon as the program is launched, before the cycle – Warpony Feb 27 '16 at 22:58
  • As suggested, using json or pickle would suffice. Use both, see what works best for your requirements. [json to file](http://stackoverflow.com/questions/12309269/how-do-i-write-json-data-to-a-file-in-python), [pickle tutorial](https://wiki.python.org/moin/UsingPickle) – idjaw Feb 27 '16 at 23:00

1 Answers1

4

(Ammended answer in light of clarifying comment: in the while true cycle i want to check, if a user's id is in 'userlist' dictionary (as a key) and if not, add it to this dictionary with value 1. Then i want to rewrite the file with a new dictionary. the file is opened as soon as the program is launched, before the cycle):

For robustly using data on disk as though it were a dictionary you should consider either one of the dbm modules or just using the SQLite3 support.

A dbm file is simply a set of keys and values stored with transparently maintained and used indexing. Once you've opened your dbm file you simply use it exactly like you would any other Python dictionary (with strings as keys). Any changes can simply be flushed and written before closing the file. This is very simple though it offers no special features for locking (or managing consistency in the case where you might have multiple processes writing to the file concurrently) and so on.

On the other hand the incredibly powerful SQLite subsystem, which has been included in the Python standard libraries for many years, allows you to easily treat a set of local file as an SQL database management system ... with all of the features you'd expect from a client/server based system (foreign keys, data type and referential integrity constraint management, views and triggers, indexes, etc).

In your case you could simply have a single table containing a single column. Binding to that database (by its filename) would allow you to query for a user's name with SELECT and add the user's name with INSERT. As your application grows and changes you could add other columns to track when the account was created and when it was most recently used or checked (a couple of time/date stamp columns) and you could create other tables with related data (selected using JOINs, for example).

(Original answer):

In general the processing of storing any internal data structure as a file, or transmitting it over a network connection, is referred to a "serialization." The complementary process of loading or receiving such data and instantiating its contents into a new data structure is referred to (unsurprisingly) as "deserialization."

That's true of all programming languages.

There are many ways to serialize and deserialize data in Python. In particular we have the native (standard library) pickle module which produces files (or strings) which are only intended or use with other processes running Python or we can, as you said, use JSON ... the JavaScript Object Notation which has become the de facto cross-language data structure serialization standard. (There are others such as YAML and XML ... but JSON has come to predominate).

The caveat about using JSON vs. Pickle is that JavaScript (and a number of other programming and scripting languages, uses different semantics for some sorts of "dictionary" (associative array) keys than Python. In particular Python (and Ruby and Lua) treats keys such as "1" (a string containing the digit "one") and 1 or 1.0 (numeric values equal to one) as distinct keys. JavaScript, Perl and some others treats the keys as "scalar" values in which strings like "1" and the the number 1 will evaluate into the same key.

There are some other nuances which can affect the fidelity of your serialization. But that's the easiest to understand. Dictionaries with strings as keys are fine ... mixtures of numeric and string keys are the most likely cause of any troubles you'll encounter using JSON serialization/deserialization in lieu of pickling.

Jim Dennis
  • 17,054
  • 13
  • 68
  • 116
  • Thank you very much. As long as this is Userlist, the keys will be strings. – Warpony Feb 27 '16 at 23:11
  • Although I didn't mention it explicitly I do want to point out that using dbm or SQLite files will allow you to efficiently use data sets that are far larger your available memory. As I said, dbm files are automatically indexed and you can create indexes on your SQLite data, are then maintained in a completely transparent manner ... implicitly by the libraries whenever your code is adding, removing, or modifying the data. – Jim Dennis Feb 27 '16 at 23:40
  • Great answer @JimDennis . Very thorough. – idjaw Feb 28 '16 at 00:28