|
|
|||||||||
|
|||||||||
| |||||||||
|
|
|
| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
#1
|
|||
|
|||
|
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? |
|
#2
|
||||
|
||||
|
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 *** |
|
#3
|
||||
|
||||
|
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 . |
|
#4
|
||||
|
||||
|
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. |
|
#5
|
|||
|
|||
|
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. |
![]() |
| Viewing: Dev Shed Forums > Programming Languages > Python Programming > Newbie: String Help |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|