I am a Python 2.7 (and programming) beginner. Also, this is my first question on SO.
I am currently building a little shopping cart application. I am storing shopping cart values in a txt file. If the user running the script has already a cart saved in the txt file from a previous session, I want the program to make the offer to continue with those items in cart.
Here is what I have so far:
my_file = open("cart.txt", "r")
match = re.findall(r'%s' % enter_name, my_file.read())
my_file.close()
#using a regular expression here to find the person's cart by name
#in cart.txt.
if match:
print "Hello %s, welcome back." % enter_name
print "Do you want to use your previous Shopping Cart?"
continue_cart = raw_input("Type 'yes' or 'no' ")
if continue_cart == "yes":
with open("cart.txt", "r") as my_file:
for line in my_file:
line_list = line.split()
print line_list
#to be continued
The final print statement is basically only a placeholder and a way for me to see what the happens. I am kinda stuck there. The code with the for loop at the end basically produces various lines with a list each. Like this
['Martin']
['Milk', '2']
['Apple', '4']
What I want to do is to "access" the values representing products & quantity in order to then add them to the shopping card class. But how to process this list in order to be able to access the values?
Any help is appreciated. Thanks.
csv,pickleorjsonto dump and load Python objects.