Home > OS >  CSV files and dicts
CSV files and dicts

Time:12-11

I have a code where children are supposed to get presents from Santa. They all have their wishes (what they wanna get), but naughty children cannot receive any presents, only those who have been nice. And so there are 3 files: nice.csv and naughty.csv, which contain children names and countries (countries don't matter) and wishlist.csv, which has names and their wishes, separated by coma.

This is my code:

class Child:
    """Child class."""

    def __init__(self, name, country):
        """Constructor."""
        self.name = name
        self.country = country

    def __repr__(self):
        """Representation."""
        return f"{self.name}"


class Gift:
    """Gift class."""

    def __init__(self, name):
        """Constructor."""
        self.name = name

    def __repr__(self):
        """Representation."""
        return f"{self.name}"


class World:
    """World class."""

    def __init__(self, nice_file, wishlist_file):
        """Constructor."""
        self.nice_file = nice_file
        self.wishlist_file = wishlist_file

    def read_file(self, filename):
        """Read file."""
        result = []
        with open(filename) as f:
            reader = csv.reader(f)
            for line in reader:
                result.append(line)
        return result

    def nice_list(self):
        """Nice children list."""
        nice = []
        for line in self.read_file(self.nice_file):
            name = Child(line[0], line[1])
            nice.append(name)
        return nice

    def wishlist(self):
        """Wishlist."""
        result = {}
        for line in self.read_file(self.wishlist_file):
            wishes = [Gift(x) for x in line[1:]]
            result[line[0]] = wishes
        return result

The problem is that naughty kids also have their wishes in the wishlist. I want naughty kids to not be in my wishlist dictionary.

For example:

nice.csv: Molly,Germany

naughty.csv: Adam,USA

wishlist.csv: Molly,doll | Adam,robot

This is what I wanna get, and I don't know how to achieve this:

{'Molly': doll}

This is what I get and is false:

{'Molly': doll, 'Adam': robot}

CodePudding user response:

import csv

class Child:
    """Child class."""

    def __init__(self, name, country):
        """Constructor."""
        self.name = name
        self.country = country

    def __repr__(self):
        """Representation."""
        return f"{self.name}"


class Gift:
    """Gift class."""

    def __init__(self, name):
        """Constructor."""
        self.name = name

    def __repr__(self):
        """Representation."""
        return f"{self.name}"


class World:
    """World class."""

    def __init__(self, nice_file, naughty_file, wishlist_file):
        """Constructor."""
        self.nice_file = nice_file
        self.naughty_file = naughty_file
        self.wishlist_file = wishlist_file

        # Read the nice and naughty children files and create sets of children's names
        self.nice_children = set()
        self.naughty_children = set()
        with open(nice_file) as f:
            reader = csv.reader(f)
            for line in reader:
                self.nice_children.add(line[0])
        with open(naughty_file) as f:
            reader = csv.reader(f)
            for line in reader:
                self.naughty_children.add(line[0])

    def wishlist(self):
        """Wishlist."""
        result = {}
        with open(self.wishlist_file) as f:
            reader = csv.reader(f)
            for line in reader:
                # Only add the child's wish to the wishlist if their name is in the set of nice children
                if line[0] in self.nice_children and line[0] not in self.naughty_children:
                    wishes = [Gift(x) for x in line[1:]]
                    result[line[0]] = wishes
        return result

CodePudding user response:

If there are multiple nice children in the wishlist.csv file, the wishlist() method in the World class that I provided will add the wishes of all of these children to the wishlist dictionary. Here is an example of how the wishlist() method works:

class World:
    # ...

    def wishlist(self):
        """Wishlist."""
        result = {}
        with open(self.wishlist_file) as f:
            reader = csv.reader(f)
            for line in reader:
                # Only add the child's wish to the wishlist if their name is in the set of nice children
                if line[0] in self.nice_children and line[0] not in self.naughty_children:
                    wishes = [Gift(x) for x in line[1:]]
                    result[line[0]] = wishes
        return result

This method reads each line in the wishlist.csv file, checks whether the child's name is in the set of nice children (and not in the set of naughty children), and then adds the child's wish to the wishlist dictionary if their name is in the set of nice children. This means that if there are multiple nice children in the wishlist.csv file, the wishes of all of these children will be added to the wishlist dictionary.

  • Related