Python Forum
Populate a table with variables
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Populate a table with variables
#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


Messages In This Thread
Populate a table with variables - by Market_Python - Jan-09-2021, 11:26 PM
RE: Populate a table with variables - by Larz60+ - Jan-10-2021, 01:45 AM
RE: Populate a table with variables - by Larz60+ - Jan-10-2021, 04:58 PM
RE: Populate a table with variables - by Pedroski55 - Jan-11-2021, 09:45 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  how to populate a dataframe thru line iteration ? knob 0 1,002 May-05-2022, 12:48 AM
Last Post: knob
  Strategy on updating edits back to data table and object variables hammer 0 1,198 Dec-11-2021, 02:58 PM
Last Post: hammer
  Populate Dropdown list from Database TommyAutomagically 4 4,530 Nov-02-2021, 05:23 PM
Last Post: ndc85430
  Auto-populate Macro variables Spartan314 3 2,661 Mar-08-2021, 12:36 AM
Last Post: Spartan314
  Python3 doesn't populate xlsx file with openpyxl Auldyin75 2 2,537 Feb-16-2021, 12:00 PM
Last Post: Auldyin75
  create new tabs and populate using python reggie4 2 2,264 Jan-23-2021, 11:25 PM
Last Post: Larz60+
  Populate MS Word from Excel data mnijs 2 7,090 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