Python Forum
How to reuse data from variables?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How to reuse data from variables?
#1
My question is rather simple and straight forward.
I've got this piece of code:

import shelve

Info=shelve.open("Info")

data={}

def add():
    while True:
        print ("User ID: ")
        usr=input(">>> ")
        print ("User Name: ")
        name=input(">>> ")
        print ("User Ager: ")
        age=input(">>> ")
        print ("User Gender: ")
        gender=input(">>> ")
        datos[usr]=[name, age, gender]
        print ("Add another entry?")
        op=input(">>> ")
        if str(op)=="s":
            del(name)
            del(age)
            del(gender)
            continue
        else:
            break
add()
Info["Data"]=data

Info.close()
And this other one:

import shelve
Info=shelve.open("Info")
pref=["Name: ", "Age: ", "Gender: "]
for i, j in Info["Data"].items():
    print (i)
    for k, l in enumerate(j):
        print ("\t"+str(pref[k]).ljust(10)+str(l).rjust(15))
    print ()
Info.close()
I know the error is in initializing the dict "data" as and empty dict every time I run the script, but I can't figure out how to store data and add any new data to the already stored.
Reply
#2
There is a nice introduction to shelve with a good example in PyMOTW3 shelve
Reply
#3
I don't know if it's ok to post the solution I came up with, but finally I was able to solve it.
So basically, I was using a new dict instead of the shelve object that behaves like a dictionary.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Create X Number of Variables and Assign Data RockBlok 8 925 Nov-14-2023, 08:46 AM
Last Post: perfringo
  Strategy on updating edits back to data table and object variables hammer 0 1,194 Dec-11-2021, 02:58 PM
Last Post: hammer
  How do use data from csv files as variables? JUSS1K 1 2,139 Oct-25-2020, 08:31 PM
Last Post: GOTO10
  How to reuse positional arguments entered in terminal. rcmanu95 1 1,872 Jul-04-2020, 01:00 AM
Last Post: bowlofred
  get and reuse the token value with huawei modem Reims 1 4,567 Oct-02-2019, 04:29 AM
Last Post: Reims
  Receive Serial Data and store in different Variables in Python jenkins43 5 5,604 Dec-28-2018, 01:33 PM
Last Post: snippsat

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020