Welcome!
This is the community forum for my apps Pythonista and Editorial.
For individual support questions, you can also send an email. If you have a very short question or just want to say hello — I'm @olemoritz on Twitter.
Beginner doubt - reading/writing a dictionary to a file
-
Hi,
I request help, to make a simple card index type of display, which can read/write the key in dictionary container into the tableview, and data into textview.
I have made a rough script which does this, but want the dictionary to be read from a file. If possible would like edit and save facility too. Thank you in advance.# Dictionary Reader For Haiku # coding: utf-8 import ui dictsample = { 'Autumn' : 'Whispering secrets\nOf summers last warm kisses\nAutumn drowns the stream.', 'Cycle' : 'The smell of decay\nIt is the smell of new life\nChange is forever', 'Growth' : 'Delicate petals\nSoftly drink the offerings\nOf nurturing clouds', 'Surrender' : 'Deserted train tracks\nSuccumb to wildflowers\nButterflies and bees' } w, h = ui.get_screen_size() def set_view_text(f): text_view = main['textview1'] text_view.text = dictsample[f] text_view.editable = False def action(sender): set_view_text(table.data_source.items[sender.selected_row]) main = ui.View() main.frame = (0,0, w, h*0.9) main.name = 'Haiku' textview1 = ui.TextView() textview1.frame = (w*0.25,0,w*0.75,h*0.9) textview1.flex = 'WH' textview1.name = 'textview1' textview1.text = "Haiku For You" textview1.background_color = 'slateblue' textview1.text_color = 'white' textview1.font = ('Avenir-Light', 16) tableview1 = ui.TableView() tableview1.frame = (0,0,w*0.25,h*0.9) tableview1.flex = 'HR' tableview1.name ='tableview1' main.add_subview(tableview1) main.add_subview(textview1) table = main['tableview1'] table_items = dictsample list_source = ui.ListDataSource(table_items) list_source.font = ('Futura-CondensedMedium', 16) list_source.text_color ='darkblue' table.data_source = table.delegate = list_source table.data_source.action = action main.present('sheet', title_bar_color = 'lavender')
-
@ramvee For simple dictionaries, (where you have one type of key, not mixed integer/strings) many people like to use the
json
module (json.load
to read andjson.dump
to write), since the resulting files are human readable and can be read by many languages/platforms.Other alternatives would include the
shelve
module, which lets you treat a file basically like a dictionary. -
I use
For read:settings = ast.literal_eval(settings_str) # convert str -> dict
For write:
settings_str = str(settings) # convert dict -> str
-
@JonB Thank You For Prompt Reply, JSON module, works perfectly for me. Grateful _/_