def extract_rules(lines):
    """ Extracts rules from file. Rules are structured in nested dictionaries.
    The outer dictionary has the color as key with a dictionary as value that contains the bag colors and amounts.
    The inner dictionary has the bag color as key and the amount as value.

    :param lines: Lines from input file
    :return: dictionary
    """
    rules = {}
    # iterate over every line
    for line in lines:
        rule = {}
        # get key and value
        key, value = line.split('contain ')
        key = key.split(' bags')[0]
        # if bag doesn't contain other bags make value None
        if value == 'no other bags.':
            rules[key] = None
        else:
            # add bags to value
            values = value.split(', ')
            for v in values:
                v = v.split(' ')
                rule['{} {}'.format(v[1], v[2])] = int(v[0])

            rules[key] = rule

    return rules