0

I am trying to read this text file and display its data. I should report product name, its value & most valuable product. I added at the end what I have tried doing so far:

Here is how the text file looks like:
item name 1 ex: hats 
quantity of item 1 ex: 12 
unit price of item 1 ex: 5.9   
item name 2 ex: jacket
quantity of item 2 ex: 13
unit price of item 2 ex: 29.9


Expected output
12 hats at 5.9 the unit. total value: 70.8
13 jackets at 29.9 the unit. total value: 390
total inventory value: 460.8
most valuable inventory: jacket: 390

what I've tried doing so far:

filename = "store.txt"

with open(filename) as f:
    content = f.read().splitlines()

content_list = [content[i * 3:(i + 1) * 3] for i in range((len(content) + 3 - 1) // 3)]

total_value = 0

for items in content_list:
    value = float(items[2])*(float(items[1]))
    print(items[1],'', items[0], '@ $', items[2], '. Value: $', value)

    total_value += value


#max_value = max(value)

print('')
print('total value: '+ str(total_value))
#print('Highest value item: ' + str(max_value))

2 Answers2

0

There are many options. Here is one (popular method) to get started:

filename = "file.py"

with open(filename) as f:
    content = f.readlines()

print(content)

You would then need to apply the logic required (to extract and manipulate the text) and write that back to a different file or provide as an output.

D.L
  • 4,339
  • 5
  • 22
  • 45
  • i tried it, however please check output: `['carrots\n', '10\n', '1.2\n', 'apples\n', '13\n', '0.6\n', 'bananas\n', '16\n', '1.7\n']` How could I remove the \n (new line) ? – Yacer Saoud Jun 18 '21 at 09:18
  • with this: https://stackoverflow.com/questions/15233340/getting-rid-of-n-when-using-readlines which in summary is `f.read().splitlines()` – D.L Jun 18 '21 at 09:25
  • thank you, that worked. I guess I will split the array into multiple array of 3 element each and do the treatment from there – Yacer Saoud Jun 18 '21 at 09:43
  • Absolutely, for this you are probably better using `pandas.read_csv()` depending on how the file is structured. – D.L Jun 18 '21 at 10:27
0

getting a particular thing sliced from the text file you can use

python re package

link if want to understand more about this package

      re.split(pattern, string, flags=0)

this will return a list

       lis = [my_list[i * n:(i + 1) * n] for i in range((len(my_list) + n - 1) // n )] 

       n=size you want to break them in 
       my_list=the list you want to break

      


     file = open("store.txt")

     lis=[]
     for line in file:
     if(line.startswith("item")):
         item_name=line[line.find(":")+1:]
         lis.append(item_name.strip())
     elif(line.startswith("quantity")):
         quantity=line[line.find(":")+1:]
         lis.append(float(quantity))
     elif(line.startswith("unit")):
         unit=line[line.find(":")+1:]
         lis.append(float(unit))

    


     content_list = [lis[i * 3:(i + 1) * 3] for i in range((len(lis) + 3 - 1) // 3)]
     print(content_list)
     total_value=0
     lis2=[]
     for items in content_list:
          value = (items[2])*(items[1])
          print(items[1],'', items[0], '@ $', items[2], '. Value: $', value)
          lis2.append(value)
          total_value+=value


      max_value = max(lis2)

      print('')
      print('total value: '+ str(total_value))
      print('Highest value item: ' + str(max_value))
justjokingbro
  • 169
  • 2
  • 13
  • is it possible to split an array into multiple arrays of 3 elements? in this sense, im trying to do this: `['carrots', '10', '1.2', 'apples', '13', '0.6', 'bananas', '16', '1.7']` `->[[carrots, 10,1.2], [apples,13,0.6]...]` – Yacer Saoud Jun 18 '21 at 09:51
  • yes it is possible,i just wrote that in my answer how you can do that – justjokingbro Jun 18 '21 at 09:58
  • I managed to do it and converted it the array to a tuple: `(('carrots', '10', '1.2'), ('apples', '13', '0.6'), ('bananas', '16', '1.7'))` Im not sure how to approach it, should I use indexes ? – Yacer Saoud Jun 18 '21 at 10:02
  • oh okay, I just saw your answer. Maybe a list will be easier to treat than a tuple – Yacer Saoud Jun 18 '21 at 10:03
  • tuple is immutable were as list is mutable so it is easier to work with list and if you want to update the values you can do that in list – justjokingbro Jun 18 '21 at 10:07
  • nevermind, i did it, ty a lot. Sadly, can't upvote cos I dont have 15 rep but you have my the upvote of my heart – Yacer Saoud Jun 18 '21 at 10:20
  • can I ask one last question. Please check my new edit. I don't know how to display the highest value. the value is equal to the last calculated value. I was thinking if I could store each value iteration into a list and there find its maximum.. I am confused. Sorry for bothering @justjokingbro – Yacer Saoud Jun 18 '21 at 10:32
  • just checking it out i will tell you – justjokingbro Jun 18 '21 at 10:47
  • good i just checked it when i posted the code – justjokingbro Jun 18 '21 at 11:39