-2
import pickle
med = {}
medfile = open("Medicines.dat","wb")
while True:
    name = input("Enter the name: ")
    company = input("Enter the company: ")
    chemical = input("Enter the chemical: ")
    price = input("Enter the price: ")
    med['name'] = name
    med['company'] = company
    med['chemical'] = chemical
    med['price'] = price
    pickle.dump(med,medfile)
    ans = input("Wouldyou like to add more(y/n) ")
    if ans == "y":
        continue
    elif ans == "n":
        break

medfile = open("Medicines.dat","r+")
print(pickle.load(medfile))
medfile.close()

The question is as follows: A binary file "Medicines.dat has structure [Name, Company, Chemical, Price] a) Write a user defined function add_data() to input the data for a record and store in the file b) Write a function search() which accepts a company name and displays the details of all the Medicines by that company

  • Hi enchantedHero, welcome to SO! Thanks for posting your code :) What error exactly are you seeing? – en_Knight Jun 16 '21 at 17:22
  • #Input Enter the name: g Enter the company: h Enter the chemical: n Enter the price: 20 Wouldyou like to add more(y/n) n Error: Traceback (most recent call last): File "main.py", line 21, in print(pickle.load(medfile)) File "/usr/lib/python3.8/codecs.py", line 322, in decode (result, consumed) = self._buffer_decode(data, self.errors, final) UnicodeDecodeError: 'utf-8' codec can't decode byte 0x80 in position 0: invalid start byte – Enchanted Hero Jun 16 '21 at 17:25
  • 1
    Great! I would add that to the question if I were you; folks usually look there first for context – en_Knight Jun 16 '21 at 17:27

1 Answers1

2

There are a few problems here:


1st Opening the file correctly

medfile = open("Medicines.dat","r+")

You mean rb. The difference is explained here, but pickle parsing requires the file to be in "binary" mode, hence the "b".


2nd Closing the file correctly

You should close the file before re-opening it for writing, as a matter of best practce. (medfile.close()). Even better, python will take care of when the file gets closed if you use the "with" syntax to create a context


3rd Having the right values

While the code should now run, I doubt it will do what you want. Your query asks "Wouldyou [sic] like to add more(y/n)", but it does not look to me like it is adding more values, since you use the same "med" dictionary over and over. Consider how the "new" fields would ever be distinguishable from the "old" ones, based on their key

en_Knight
  • 5,301
  • 2
  • 26
  • 46
  • Could you tell me how to do it with a list? – Enchanted Hero Jun 16 '21 at 17:45
  • As in 'med = {}' – Enchanted Hero Jun 16 '21 at 17:50
  • Let's see, 'med = {}' makes a *dictionary*. A dictionary stores a key/value pair, which let's you do things like med['name'] = 'dogman. The *key* in that example is 'name', and the 'value' is 'dogman'. I would strongly recommend doing some reading on basic data structures (I find the official docs a little dense for a noobie, but there's a ton of great resources on the web. If your learning python, i bet your teacher has some recs) – en_Knight Jun 16 '21 at 18:00
  • A *list* is a just an ordered collection of things. a = ['A','B','C'] . a[0] is A, a[1] is 'B', so on. There's no "mapping" between anything. In your case, it might make sense to have a *list of dictionaries*. Where you've written 'med = {}', you could make a list, 'med = []'. Then in the loop, (after while True), make a new dictionary, 'new_entry = {}', and put your key value pairs in it. Before writing to the database, you could add the new entry to your list ('list.append(new_entry)'), and then write out the list instead of the individual entry – en_Knight Jun 16 '21 at 18:02
  • But that's one of many many ways, it may be quite helpful to take a look at the datastructures and come up with a solution that makes sense to you :) – en_Knight Jun 16 '21 at 18:02