Python Forum

Full Version: Populate a table with variables
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hi guys,

I need your help please to solve a very basic problem in Python (I think it's simple, but still challenging for me as a beginner Huh ).

I have created a view variables, say Price_a and Price_b.

Price_a = 8.50
Price_b = 10
In a next step, I'd like to create a table that shows 'A' and 'B' in the rows and 'price' in the header, so I can populate it with the variables Price_a and Price_b. I generally know how to create tables with numbers, but not how to insert pre-defined variables in such a table.

Any help would be greatly appreciated.

Many thanks,

Peter
read about dictionaries.
example:
food_prices = {
    'Vegetables': {
        'Carrots': {
            'price': 1.50,
        },
        'Onions': {
            'price': 1.99,
            'unit': 'lb'
        }
    },
    'Fruit': {
        'Orange': {
            'price': 5.50,
            'unit': 'dozen'
        },
        'Apples': {
            'price': 4.95,
            'unit': 'dozen'
        }
    }
}

# display dictionay data
def display_dict(dictname, level=0):
    indent = " " * (4 * level)
    for key, value in dictname.items():
        if isinstance(value, dict):
            print(f'\n{indent}{key}')
            level += 1
            display_dict(value, level)
        else:
            print(f'{indent}{key}: {value}')
        if level > 0:
            level -= 1


display_dict(food_prices)
when run:
Output:
Vegetables Carrots price: 1.5 Onions price: 1.99 unit: lb Fruit Orange price: 5.5 unit: dozen Apples price: 4.95 unit: dozen
Great, many thanks Larz60+!
added code at bottom on how to access single item
food_prices = {
    'Vegetables': {
        'Carrots': {
            'price': 1.50,
        },
        'Onions': {
            'price': 1.99,
            'unit': 'lb'
        }
    },
    'Fruit': {
        'Orange': {
            'price': 5.50,
            'unit': 'dozen'
        },
        'Apples': {
            'price': 4.95,
            'unit': 'dozen'
        }
    }
}

# display dictionary data

def display_dict(dictname, level=0):
    indent = " " * (4 * level)
    for key, value in dictname.items():
        if isinstance(value, dict):
            print(f'\n{indent}{key}')
            level += 1
            display_dict(value, level)
        else:
            print(f'{indent}{key}: {value}')
        if level > 0:
            level -= 1


display_dict(food_prices)

apple_price = food_prices['Fruit']['Apples']['price']
apple_unit  = food_prices['Fruit']['Apples']['unit']

print(f"\nApple price is {apple_price} per {apple_unit}")
Output:
Vegetables Carrots price: 1.5 Onions price: 1.99 unit: lb Fruit Orange price: 5.5 unit: dozen Apples price: 4.95 unit: dozen Apple price is 4.95 per dozen
I just read the word "table". You want a table? Spreadsheet or html?

Obviously, Larz60+ knows json and you can import the json module and use json.dumps() to convert his dictionary to a string, then write the string to a file.

Like you, I am really just a beginner and only know simple stuff.

Save your data as text with csv format (basically, a comma separates column values) and you can open it in any spreadsheet program.

So, beginner stuff, easy to understand what's happening:

path = '/home/pedro/winter2020/20PY/dumpFiles/'

item = 'X'
goods = []
headers = ('Item', 'Buy', 'Sell')
goods.append(headers)

while True:
    print('Enter nothing to quit.')
    item = input('What goods are we talking about? ')
    if item == '':
        break
    buy_price = input('Enter the purchase price. ')
    sell_price = input('Enter the sales price. ')
    tup = (item, buy_price, sell_price)
    goods.append(tup)

output = ''

for tup in goods:
    string = tup[0] + ',' + tup[1] + ',' + tup[2] + '\n'
    output = output + string

savefilename = path + 'prices.csv' # you can open this in any spreadsheet program

file = open(savefilename, 'w')
file.write(output)
file.close()