Search code examples
pythonshelve

Python - Save content of a file txt in a db with module shelve


I have one text file called "actors.txt" and "actresses.txt". This files weight 200MB and 100MB. I want save the content in a db doing "import shelve". The content is organized of the next way:

Last name, first name, films date, role

And I want to save this information for running various queries, for example, make a function that given an actor, will print all his movies.

I tried different's ways like:

filename_actors=('actors.txt')

database_actores=shelve.open(filename_actors)

But it doesn't work. How do I do it?


Solution

  • Shelve is a terrible module (both slow and insecure), and you should never use it.

    If you're looking for an easy-to-use in-memory (or file-backed) database that's part of the Python standard library, take a look at the sqlite3 module: http://docs.python.org/2/library/sqlite3.html

    It's rock solid, powerful (it's a full-fledged SQL database), well-documented and remarkably fast.

    EDIT: Also, as @roippi said, to actually read your files, you'll need to use the csv module: http://docs.python.org/2/library/csv.html