943,771 Members | Top Members by Rank

Ad:
  • Python Discussion Thread
  • Marked Solved
  • Views: 6806
  • Python RSS
Sep 19th, 2008
0

How to get rid of newline char

Expand Post »
Hi,

Below is my test code, i have a list of names in a file, i read the names and i have to insert them as menu options in the menubar. Since its a dynamic menu and people can add new menu's it has to be read from a file everytime its loaded. Problem is that its appending an extra character after everyname in the menu, something line '[]'(check the attached screen shot) which i assume is the newline char, since i'm inserting that too... How can i get rid of that, any suggestions? i tried reading and writing the file in binary mode but makes no difference.


python Syntax (Toggle Plain Text)
  1. from Tkinter import *
  2.  
  3. fileObj = open("c:/test","w")
  4. fileObj.write("rajat")
  5. fileObj.write("\n")
  6. fileObj.write("prasun")
  7. fileObj.close()
  8.  
  9. file1 = open("c:/test","r")
  10. lines = file1.readlines()
  11. file1.close()
  12.  
  13. root = Tk()
  14. menubar = Menu(root)
  15. filemenu = Menu(menubar)
  16. menubar.add_cascade(label="Names",menu=filemenu)
  17.  
  18. for line in lines:
  19. filemenu.add_cascade(label=line)
  20.  
  21. root.config(menu=menubar)
  22. root.mainloop()
Attached Images
File Type: bmp tk.bmp (39.5 KB, 55 views)
Last edited by Agni; Sep 19th, 2008 at 6:56 am.
Featured Poster
Reputation Points: 431
Solved Threads: 116
Practically a Master Poster
Agni is offline Offline
654 posts
since Dec 2007
Sep 19th, 2008
1

Re: How to get rid of newline char

Use string method strip() to remove newline characters.
Python Syntax (Toggle Plain Text)
  1. for line in lines:
  2. filemenu.add_cascade(label=line.strip())
Reputation Points: 86
Solved Threads: 40
Junior Poster
solsteel is offline Offline
141 posts
since Mar 2007
Sep 19th, 2008
0

Re: How to get rid of newline char

simple and exact ... worked perfectly... Thanks a lot.
Featured Poster
Reputation Points: 431
Solved Threads: 116
Practically a Master Poster
Agni is offline Offline
654 posts
since Dec 2007

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Python Forum Timeline: Socket Stuff
Next Thread in Python Forum Timeline: Python Help for a newcomer





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC