Python Forum
Populate a table with variables
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Populate a table with variables
#1
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
Reply
#2
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
Market_Python likes this post
Reply
#3
Great, many thanks Larz60+!
Reply
#4
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
Reply
#5
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()
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Auto-populate Macro variables Spartan314 3 489 Mar-08-2021, 12:36 AM
Last Post: Spartan314
  Python3 doesn't populate xlsx file with openpyxl Auldyin75 2 286 Feb-16-2021, 12:00 PM
Last Post: Auldyin75
  create new tabs and populate using python reggie4 2 252 Jan-23-2021, 11:25 PM
Last Post: Larz60+
  Fetch Oracle DB rows & print it in HTML file with table's col headers in table format tssr_2001 1 671 Sep-04-2020, 01:39 PM
Last Post: ibreeden
  Mysql CREATE TABLE IF NOT EXISTS dynamic table name nisusavi 0 516 Apr-29-2020, 06:45 PM
Last Post: nisusavi
  MYSQL how to assign a table for each user in my username table database? YoshikageKira 1 1,006 Dec-26-2019, 05:57 AM
Last Post: buran
  Populate MS Word from Excel data mnijs 2 4,420 Oct-15-2017, 01:04 PM
Last Post: buran

Forum Jump:

User Panel Messages

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