Hello! I’m beginner with Python (I’ve completed only 38 % of “Python for Everybody” course), and english isn’t my native tongue (I’ll try to be clear!).
I’m trying to code a program to read .txt files of shopping receipts in order to analyze them and draw conclusions. To begin with that, I thought it was necessary to create a dictionary with items and its prices.
I wrote a function that receives a list with open files as parameter, and returns the dictionary:
def supermarket_products(open_files): # open_files is a list containing open .txt files of supermarket receipts.
items = []
prices = []
for file in open_files:
for line in file:
line = line.rstrip()
# In each receipt, the lines containing items don't include $ or the spanish word OFERTA.
if "$" not in line and "OFERTA" not in line:
word_list = line.split()
# In a line, the first "word" is a numerical code, and the last one is the price.
# The words in between are the item's name.
item_words_list = word_list[1:len(word_list) - 1]
item = ""
for word in item_words_list: # Turning words from the list to a single string.
item = item + " " + word
items.append(item.lstrip()) # Adding item to list.
# Now, extracting the price.
price = word_list[-1]
price = int(price.replace(".", "")) # In receipts, prices contain a dot.
prices.append(price)
dictionary = dict(zip(items, prices)) # Using the lists to create a dictionary.
return dictionary
Then, when I call the function, it returns an empty dictionary ({}). Why is this happening, and how can I fix it? I’m sorry if I made any typing or syntax mistake. Thank you in advance for your help!