Python Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me
Go Back   Dev Shed ForumsProgramming LanguagesPython Programming

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old June 23rd, 2004, 03:36 PM
bquad20 bquad20 is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jun 2004
Posts: 13 bquad20 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Newbie: String Help

I would like to put some information together that lives between 2 # signs. Currently I am using splitlines to break it apart, but I am having trouble getting it all together.

code:
cron = "parse.txt"
file = open(cron)
list = []

while (1):
line = file.read()
if not line : break
list = line

if list.find('#'):
list = list.splitlines()
print list

.txt file
# Record the current crontab daily
#
2 0 * * * $HOME/bin/rcrdCron.ssh >> $HOME/rcrdCron.lg
#

would like to see:
Record the current crontab daily 2 0 * * * $HOME/bin/rcrdCron.ssh >> $HOME/rcrdCron.lg

is this possible?

Reply With Quote
  #2  
Old June 23rd, 2004, 08:11 PM
Grim Archon's Avatar
Grim Archon Grim Archon is offline
Mini me.
Dev Shed Novice (500 - 999 posts)
 
Join Date: Nov 2003
Location: Cambridge, UK
Posts: 783 Grim Archon User rank is Corporal (100 - 500 Reputation Level)Grim Archon User rank is Corporal (100 - 500 Reputation Level)Grim Archon User rank is Corporal (100 - 500 Reputation Level)Grim Archon User rank is Corporal (100 - 500 Reputation Level)  Folding Points: 1488 Folding Title: Novice Folder
Time spent in forums: 3 Days 2 h 15 m 57 sec
Reputation Power: 7
Send a message via MSN to Grim Archon
Please don't be put off by my comments from exploring python or posting questions but it would have helped if you had read the Sticky messages at the top of the forum

Because you have not used code tags the layout has not been preserved so I cannot comment on your solution as a whole.

I don't mean to assasinate your code but there are a number of other problems:
You have redefined the built in list type as itself
Code:
list = []

You should avoid re-using Pythons reserved words like this. Call it mylist or something.
It also morphs to a string and back to a list again at different places
You also redefine file (file is the same as open in python).

The while loop is not needed because the read() reads all characters from a file.
Why not use
Code:
if line:

.find() returns -1 if the test string is not found
so the test test would be
Code:
if list.find('#') != -1: 


If the log file only contains what you posted then you could use:
Code:
text = myfile.read()
text = text.replace("\n#\n"," ")
text = text.replace("#","")
text = text.strip()
print text


By the way, if you were trying to join a list together then:
"".join(mylist)
is a good trick.

If the log file actaully contained multiple entries then I would suggest investigating the re regular expressions module.

grim
__________________
*** Experimental Python Markup CGI V2 ***

Reply With Quote
  #3  
Old June 24th, 2004, 01:39 AM
netytan's Avatar
netytan netytan is offline
Hello World :)
Dev Shed Frequenter (2500 - 2999 posts)
 
Join Date: Mar 2003
Location: Hull, UK
Posts: 2,536 netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level) 
Time spent in forums: 1 Week 2 Days 18 h 2 m 16 sec
Reputation Power: 63
Send a message via ICQ to netytan Send a message via AIM to netytan Send a message via MSN to netytan Send a message via Yahoo to netytan
Seems to me that you are trying to add a comment like facility to your program, correct me if i'm wrong on that but heres what you want to be doing.

Code:
#!/usr/bin/env python

jobs = []

for line in file('cron.txt'):
    if not line.startswith('#'): jobs.append(line.strip())

#This will give you all the lines in the file that dont
#begin with the '#' token collected together in the
#'jobs' list with any trailing white space removed.

...do whatever with these jobs...


Note: this is untested but it should work fine , if there are any problems just let me know.

Hope this helps,

Mark.

P.S. check out the sticky on how to ask a question at the top of this forum like Grim sugested .
__________________
programming language development: www.netytan.com Hula


Reply With Quote
  #4  
Old June 24th, 2004, 01:42 AM
netytan's Avatar
netytan netytan is offline
Hello World :)
Dev Shed Frequenter (2500 - 2999 posts)
 
Join Date: Mar 2003
Location: Hull, UK
Posts: 2,536 netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level) 
Time spent in forums: 1 Week 2 Days 18 h 2 m 16 sec
Reputation Power: 63
Send a message via ICQ to netytan Send a message via AIM to netytan Send a message via MSN to netytan Send a message via Yahoo to netytan
Just realised you can also do this as a list conprehension with little to no worried .

Code:
#!/usr/bin/env python

jobs = [line.strip() for line in file('cron.txt') if not line.endswith('#')]


This will do exactly the same thing so i've left out the comments oin this one.

Mark.

Reply With Quote
  #5  
Old June 24th, 2004, 12:45 PM
bquad20 bquad20 is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jun 2004
Posts: 13 bquad20 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Wink

thank you all for your help!

i was able to take a little bit for each code snippet
and incorporate/learn from it in my program. this is
a good board & hope to keep learning from other
python users.

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesPython Programming > Newbie: String Help


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


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

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 3 hosted by Hostway