June 23rd, 2004, 03:36 PM
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.
cron = "parse.txt"
file = open(cron)
list = 
line = file.read()
if not line : break
list = line
list = list.splitlines()
# 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?
June 23rd, 2004, 08:11 PM
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
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
.find() returns -1 if the test string is not found
so the test test would be
If the log file only contains what you posted then you could use:
if list.find('#') != -1:
By the way, if you were trying to join a list together then:
text = myfile.read()
text = text.replace("\n#\n"," ")
text = text.replace("#","")
text = text.strip()
is a good trick.
If the log file actaully contained multiple entries then I would suggest investigating the re regular expressions module.
June 24th, 2004, 01:39 AM
June 24th, 2004, 01:42 AM
Just realised you can also do this as a list conprehension with little to no worried .
This will do exactly the same thing so i've left out the comments oin this one.
jobs = [line.strip() for line in file('cron.txt') if not line.endswith('#')]
June 24th, 2004, 12:45 PM
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