LinuxQuestions.org
Review your favorite Linux distribution.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
LinkBack Search this Thread
Old 09-12-2006, 09:27 AM   #1
tangle
Senior Member
 
Registered: Apr 2002
Location: Holmesville, Ohio
Distribution: Slackware
Posts: 1,721

Rep: Reputation: 52
Removing a colon and all characters before it, in a python list


I am new to python, so this may be a stupid question.

I am working on a GUI application to configure idesk. The config file is laid out like this.
Code:
table Config
 FontName: tahoma
 FontSize: 8
 FontColor: #ffffff
 ToolTip.FontSize: 9
 ToolTip.FontName: gothic
I appended each line to a list using:
Code:
for line in inFile.readlines():
    if empty.match(line):
        continue
    else:
        list.append(resub('\n','',line))
The list prints out fine, but I want to have only the characters after the colon, which are the program's values. I woul dlike to load the current values in to an input box so the user knows what they are. I tried to get it to remove the colon when I appended the items to the list, but couldn't figure out how to.

So I decided the easiest way was to parse the list and remove the characters, but I am having problems. I can remove the colon or any other character using this:
Code:
listing = [item.replace(':','') for item in list)
I tried adding an astrics before the colon, but that didn't work. any help would be appreciated.
 
Old 09-12-2006, 03:10 PM   #2
Proud
Senior Member
 
Registered: Dec 2002
Location: England
Distribution: Used to use Mandrake/Mandriva
Posts: 2,794

Rep: Reputation: 116Reputation: 116
I haven't used Python in ages, but you need to take each line, find the index/position of the colon, and then slice from there to the end of the line. Check out either String or RegularExpression (RegEx) libraries for a function to find the colon's index, and then brush up on Strings/slicing to copy just the end of the line you want. Hope this helps.

Edit: from here, rsplit() should return the string you want as the first item in the list it returns.

Edit2: It may be faster to do i = line.index(":") and then line[i:]. Rather than rsplit() generating a list of pieces of the line.

Last edited by Proud; 09-12-2006 at 03:29 PM.
 
Old 09-12-2006, 05:20 PM   #3
tangle
Senior Member
 
Registered: Apr 2002
Location: Holmesville, Ohio
Distribution: Slackware
Posts: 1,721

Original Poster
Rep: Reputation: 52
I figured it out.
Code:
list.append(re.sub('^.*: ', '', prefs))
I had to have the period in there. Once I did that everything from the whitespace after the colon to the the beginning of the line was removed.

Thanks for the post.
 
Old 09-12-2006, 06:59 PM   #4
ghostdog74
Senior Member
 
Registered: Aug 2006
Posts: 2,695
Blog Entries: 5

Rep: Reputation: 237Reputation: 237Reputation: 237
you could use the split() function to split on colons.
 
Old 09-12-2006, 09:48 PM   #5
Dan04
Member
 
Registered: Jun 2006
Location: Texas
Distribution: Ubuntu
Posts: 207

Rep: Reputation: 36
line.split(':', 1)[-1]
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Could not init font path element unix/:7100, removing from list! jeffreybluml Fedora 18 10-12-2011 08:06 AM
Removing Characters when outputting to a script thedarkdestroyer Linux - Newbie 3 03-21-2006 03:13 PM
AIX colon files rayudu AIX 2 07-09-2005 05:51 PM
need a semi colon .. but vert spooge Linux - General 4 09-19-2004 03:52 PM
How To Input Characters In A List bprasanth_20 Programming 10 10-30-2003 12:55 AM


All times are GMT -5. The time now is 05:13 AM.

Main Menu
 
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: @linuxquestions
Open Source Consulting | Domain Registration