Python Forum
create a list of object with a list of character
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
create a list of object with a list of character
#1
Hello i am french student and i work on a first python 3.7 project

My problem is the following
i have create and object "ingredient" with 4 parameter

Now i have to create a list of object "ingredient" with information contain in a file .TXT
i have done the following programm but the list of object isn't generate

here are some sample of my code :

1- My object "ingredient"
 def __init__(self, nom: str, quantite: int, unite: str, vegetarien: str) -> None:
        # Crée un nouvelle ingrédient. pour le nom, quantite, unite, vegetarien

        self._nom = nom
        self._quantite = quantite
        self._unite = unite
        self._vegetarien = vegetarien
2- "open file txt and create a first list from the file"
 liste_ingredients = []
    with open("stock.txt", "r") as f:
        for line in f.readlines():
            # print(line)
            liste_ingredients.append(line.split(";"))
    del liste_ingredients[0]

    print(liste_ingredients)
3- The part that doesn't work the goal was to generate a list of object in placard list"
placard = []

    for i in liste_ingredients:
        i[0] = ingredient(i[0], i[1], i[2], i[3])
        placard.append(i[0])

    print(placard)
Thanks a lot for your answer and have a good day ;)

Finally we find our problem

i only had to say when it is int type
i[0] = ingredient(i[0], int(i[1]), i[2], i[3])
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  No matter what I do I get back "List indices must be integers or slices, not list" Radical 4 1,157 Sep-24-2023, 05:03 AM
Last Post: deanhystad
  Possible to create an object inside another object that is visible outside that objec MeghansUncle2 17 2,200 May-25-2023, 02:08 PM
Last Post: MeghansUncle2
  Delete strings from a list to create a new only number list Dvdscot 8 1,511 May-01-2023, 09:06 PM
Last Post: deanhystad
  List all possibilities of a nested-list by flattened lists sparkt 1 915 Feb-23-2023, 02:21 PM
Last Post: sparkt
  add object and name in list 3lnyn0 4 1,274 Nov-24-2022, 07:33 PM
Last Post: buran
  Сheck if an element from a list is in another list that contains a namedtuple elnk 8 1,833 Oct-26-2022, 04:03 PM
Last Post: deanhystad
  [split] why can't i create a list of numbers (ints) with random.randrange() astral_travel 7 1,508 Oct-23-2022, 11:13 PM
Last Post: Pedroski55
Question Keyword to build list from list of objects? pfdjhfuys 3 1,557 Aug-06-2022, 11:39 PM
Last Post: Pedroski55
  Split a number to list and list sum must be number sunny9495 5 2,279 Apr-28-2022, 09:32 AM
Last Post: Dexty
  AttributeError: 'list' object has no attribute 'upper' Anldra12 4 4,872 Apr-27-2022, 09:27 AM
Last Post: Anldra12

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020