Create selection list from a file in PyS60

Let's come back to the project - DSE price tracker. The next step was to load all the company names from a file (stored in the mobile phone memory) and display in the selection list. The file contains the company names, one per line.

So I wrote the following code:

import appuifw

def get_data_from_file():
fp = open('c:\\names.txt', 'r')
li = fp.readlines()
fp.close()
compList = []
for item in li:
item = item.decode("utf-8")
compList.append(item)
return compList

L = get_data_from_file()
index = appuifw.selection_list(choices=L , search_field=1)

Yes, it works. Note that you must decode each line to unicode (utf-8). (item = item.decode("utf-8"))



But wait, what are those small boxes beside each name? I guess it's the newline character ('\n') after each line. So I need to replace '\n' with a blank ''.
Here is the modified get_data_from_file()

def get_data_from_file():
fp = open('c:\\names.txt', 'r')
li = fp.readlines()
fp.close()
compList = []
for item in li:
item = item.replace('\n', '')
item = item.replace('\r', '')
item = item.decode("utf-8")
compList.append(item)
return compList

Now it looks fine!



Next to-do is to grab the file from a remote URL and store it in mobile phone memory, then display the list.

Comments

Popular posts from this blog

Python all any built-in function

Accept-Encoding 'gzip' to make your cralwer faster

lambda magic to find prime numbers