×

Discussion Board

Results 1 to 4 of 4
  1. #1
    Registered User
    Join Date
    Nov 2008
    Posts
    3

    Question Display a text file (as a list)

    Hi everyone,

    I'm all new with python language (and not really advanced in programming stuff in general).

    Anyway, I tried to program a very small application in python s60, (I simulate it with the nokia simulator at the moment), to make a light "shopping list".

    So far, my program asks the user to choose some items from a list and records the choice in a text file (which is recorded in a special directory dedicated to the app.

    Code:
    list_tobuy = [u"Bread", u"Water", ...]
            choicetobuy = appuifw.multi_selection_list (list_tobuy, style='checkbox', search_field=1)
            for x in choicetobuy:
                print >> ListFile
    Then, in a second time, I would like to be able to go back to the user menu choice (create a new list, or use a new list), and if the "use a new list " choice is made, open the text file recorded previously and display is to the screen with the list form (with checkbox if possible).

    To do so, I wrote the following code :
    Code:
    ListShop = file (u"D:\\Data\\ShopList\\CurrentList.txt", "r+")
         ListShop.seek(0)
         ListShopG = ListShop.readlines()
         appuifw.multi_selection_list(ListShopG, style='checkbox', search_field=1)
    But unfortunately, the screen displays a blank list saying "No data". I think that the list "ListShopG" can't display the items recorded in the file because the "readlines()" doesn't add the "u" before the name of the items

    Has anyone an idea to solve it ?

    Feel free to tell me if I haven't been clear enough.

    Below, you can see the whole code that I wrote (I know there are a lot of mistakes, but I'm a real beginner ).

    Code:
    #Import of the libraries
    import appuifw, e32, os, os.path
    appuifw.app.screen= "normal"
    
    #Enable Exit key
    def quit():
        print "Exit key pressed!"
        app_lock.signal()
    
    appuifw.app.exit_key_handler = quit
    
    #Presentation/Introduction
    name = appuifw.query(u"Type your name:", "text", u"My Name is")
    appuifw.note(u"Welcome to your shopping list Assistant: " + str(name) + u" !" , "conf") #"info", "error" and "conf
    
    #Directory creation for the data storage
    PATH = u"D:\\Data\\ShopList"
    if not os.path.exists(PATH):
        os.makedirs(PATH)
    
    #from os import chdir
    #chdir(u"C:\Symbian\8.1a\S60_2nd_FP3\Epoc32\winscw\d\Data\ShopList")
        
    #Application Title
    appuifw.app.title = u"Shopping List Assistant"
    
    ######Menu Creation
    liste_todo = [u"Start New list", u"Use Set up list"]
    choixmenu = appuifw.selection_list(liste_todo)
    
    ######New List Mode
    if choixmenu == 0:
        #Txt file creation for the list
        ListFile = file (u"D:\\Data\\ShopList\\CurrentList.txt", "w+")
        ListFile.write(u"[") ##
        appuifw.note(u"New List Mode", "info")
        choix = 0
        while choix != 1:
            list_tobuy = [u"Bread", u"Water", u"Nutella", u"Programming_skills"]
            choicetobuy = appuifw.multi_selection_list (list_tobuy, style='checkbox', search_field=1)
            for x in choicetobuy:
                #print >> ListFile, list_tobuy[x] #( "u\"" +list_tobuy[x] + "\n\" ")
                ListFile.write( "u\"" +list_tobuy[x] + "\n\", ")
            choix = appuifw.query(u"Add some greetings?", "number")
        ListFile.write(u"]") ##
        ListFile.seek(0)
        ListFile.close()
        appuifw.note(u"Good Bye !", "info")
        
    ######Use List Mode
    elif choixmenu == 1:
         appuifw.note(u"Use List Mode", "info")
         ListShop = file (u"D:\\Data\\ShopList\\CurrentList.txt", "r+")
         ListShop.seek(0)
         ListShopG = ListShop.readlines()
         #print ListShopG
         appuifw.multi_selection_list(ListShopG, style='checkbox', search_field=1)
         #appuifw.selection_list(u"You have selected in your shopping list: " +ListShop.read() , "info")
         ListShop.close()
        
    
    ######We want to live the application     
    else:
        appuifw.note(u"Good Bye !", "conf")
    As you can see, I also tried to write in the text file the items selected as they should have been written in the code, thinking that if I used the read() function, it would recognise it as a list... (but didn't work either )

    Many thanks

    Ps : Forgive my poor english, but I'm french
    Last edited by pierre_remi; 2008-11-08 at 22:23.

  2. #2
    Registered User
    Join Date
    Nov 2008
    Posts
    3

    Re: Display a text file (as a list)

    Hi again

    I actually found the main problem. Indeed, the screen was displaying "No Data" because the string I was trying to display was not in "Unicode".

    I then converted my string in as Unicode, but it means that I can't use the "readlines()" anymore as it doesn't belong to the Unicode.

    Nevertheless, I still want to classify the different words of my string (which are now converted as unicode) in that way [u"Item 1", u"Item2",...]

    So my question is : Is there a function which detects each word of a unicode string (separated with a space), and sort them in a list ?

    Thanks again

  3. #3
    Super Contributor
    Join Date
    Oct 2007
    Location
    Deva, Romania
    Posts
    3,471

    Re: Display a text file (as a list)

    Hi,

    You could use the split(separator) method which works for both regular strings and unicode ones. For example:
    Code:
    s=u"Item1 Item2 Item3"
    new_s=s.split(" ") #Separate at each space
    print new_s
    #Will show [u'Item1', u'Item2', u'Item3']
    Last edited by bogdan.galiceanu; 2008-11-09 at 16:04. Reason: Typo

  4. #4
    Registered User
    Join Date
    Nov 2008
    Posts
    3

    Re: Display a text file

    That was exactly the function I was looking for !

    Thanks a lot !

    Ps : Instead of using this line to write in my text file :

    Code:
    print >> ListFile, list_tobuy[x]
    I had to use this kind of line :
    Code:
    ListFile.write (list_tobuy[x] + " ")
    Everything's fine for me now, I'm just displayling a blank line in the shopping list, but I think, I'll be able to fixe pretty quickly this problem.

    I finally, used the method : MyList.pop() to delete the last element of my list which was a useless space.

    Thx again and hang in there !
    Last edited by pierre_remi; 2008-11-10 at 10:17.

Similar Threads

  1. How to append text in a text file in j2me
    By lovely_334801 in forum Mobile Java Networking & Messaging & Security
    Replies: 4
    Last Post: 2010-04-26, 11:10
  2. Replies: 1
    Last Post: 2007-12-14, 05:53
  3. Help needed with a 3gp file...
    By joedoe_1981 in forum Streaming and Video
    Replies: 0
    Last Post: 2007-07-18, 18:58
  4. How to display text from a file
    By sblider in forum Symbian
    Replies: 2
    Last Post: 2004-08-03, 15:35
  5. Help about display a long text file
    By pop954 in forum Mobile Java General
    Replies: 2
    Last Post: 2003-11-18, 08:24

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
×