| | |
How to get rid of newline char
Thread Solved |
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.
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)
from Tkinter import * fileObj = open("c:/test","w") fileObj.write("rajat") fileObj.write("\n") fileObj.write("prasun") fileObj.close() file1 = open("c:/test","r") lines = file1.readlines() file1.close() root = Tk() menubar = Menu(root) filemenu = Menu(menubar) menubar.add_cascade(label="Names",menu=filemenu) for line in lines: filemenu.add_cascade(label=line) root.config(menu=menubar) root.mainloop()
Last edited by Agni; Sep 19th, 2008 at 6:56 am.
thanks
-chandra
-chandra
•
•
Join Date: Mar 2007
Posts: 110
Reputation:
Solved Threads: 31
Use string method strip() to remove newline characters.
Python Syntax (Toggle Plain Text)
for line in lines: filemenu.add_cascade(label=line.strip())
![]() |
Similar Threads
- how to extract column from text or cel file (C)
- Can anyone help me with adding 2 polynomials together? (C++)
- cleaning a two-dimensional array of newlines (C)
- Hey, I got a very simple program here. Yet the compiler is disagreeing with me. (C++)
- My own read function (C)
- removing the null terminator at the end of string (C)
- Problems with C++ program (C++)
Other Threads in the Python Forum
- Previous Thread: Socket Stuff
- Next Thread: Python Help for a newcomer
| Thread Tools | Search this Thread |
abrupt accessdenied anti apache application approximation argv array beginner book builtin calculator change converter countpasswordentry curved dan08 dictionaries dictionary dynamic edit enter examples file float format function gui homework import inches input java keyboard lapse launcher library line lines linux list lists loop microphone mouse movingimageswithpygame mysqlquery newb number numbers numeric output parameters parsing path phonebook plugin port prime programming projects py2exe pygame pyopengl pysimplewizard python random recursion redirect remote reverse scrolledtext session simple software sprite statictext string strings syntax table terminal text textarea thread threading time tlapse trick tuple tutorial twoup ubuntu unicode unit urllib urllib2 variable wordgame wxpython





