0

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.

3
  • using texts files for this sort of thing is so much hard work. Please consider using an RDBMS instead. Commented Jan 30, 2017 at 13:42
  • 3
    What the sicilian @e4c5 said! If you use a text file, however, consider using a serialization library like csv, pickle or json to dump and load Python objects. Commented Jan 30, 2017 at 13:44
  • Thanks yeah this is really just right now, to get started. I expected this to not be a state of the art approach. I'll looking into RDBMS for sure. Commented Jan 30, 2017 at 13:45

1 Answer 1

1

As e4c5 said by not utilizing more advanced concepts (a database, or just using a simple json or pickle) you are making it much harder for yourself.

If you want to use only plain text files directly to store and load values not only it's much harder to write the code (honestly, i've been programming for years and I still would not be confident I got that right!) but also for everyone (including yourself in the future) to read the code. And maintainability is really - from experience - the holly grail of programming. That's why we use python after all!

That said, I understand, you are not trying not build a perfect solution but to learn programming!

That said, your cart.txt probably looks something like this:

Martin
Milk 2
Apple 4
Eva
Milk 1

Then your for loop would look like this (for example, but as I said, this is not a right approach and I it is very fragile and buggy!):

enter_name_found = False
cart_items = {}
for line in my_file:
    line_list = line.split()
    if len(line_list) == 1:
        if line_list[0] == enter_name:
            enter_name_found = True
        else:
            if enter_name_found:
                break
    else:
        if enter_name_found:
            cart_items[line_list[0]] = line_list[1]
print(cart_items)

EDIT:

To demonstrate how much simpler this all is using a database:

Here is a full example to save a cart:

import shelve

cart = shelve.open("cart.dat")
cart["Martin"] = {"Apple": 5, "Milk": 2}
cart["Eva"] = {"Milk": 1, "Cat": 4}
cart.close()

And full example to load the cart from the DB:

import shelve
cart = shelve.open("cart.dat")

print(cart["Martin"])
print(cart["Eva"])
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks a lot! This is very convincing. I will not waste any more time with building code that relies on data from a .txt and will start to look into databases instead.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.