How to get rid of newline char

Thread Solved

Join Date: Dec 2007
Posts: 443
Reputation: Agni is a jewel in the rough Agni is a jewel in the rough Agni is a jewel in the rough 
Solved Threads: 68
Sponsor
Agni's Avatar
Agni Agni is offline Offline
Posting Pro in Training

How to get rid of newline char

 
0
  #1
Sep 19th, 2008
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.


  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()
Last edited by Agni; Sep 19th, 2008 at 6:56 am.
Attached Images
File Type: bmp tk.bmp (39.5 KB, 2 views)
thanks
-chandra
Reply With Quote Quick reply to this message  
Join Date: Mar 2007
Posts: 110
Reputation: solsteel is on a distinguished road 
Solved Threads: 31
solsteel solsteel is offline Offline
Junior Poster

Re: How to get rid of newline char

 
1
  #2
Sep 19th, 2008
Use string method strip() to remove newline characters.
  1. for line in lines:
  2. filemenu.add_cascade(label=line.strip())
Reply With Quote Quick reply to this message  
Join Date: Dec 2007
Posts: 443
Reputation: Agni is a jewel in the rough Agni is a jewel in the rough Agni is a jewel in the rough 
Solved Threads: 68
Sponsor
Agni's Avatar
Agni Agni is offline Offline
Posting Pro in Training

Re: How to get rid of newline char

 
0
  #3
Sep 19th, 2008
simple and exact ... worked perfectly... Thanks a lot.
thanks
-chandra
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC