#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2004
    Posts
    13
    Rep 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?
  2. #2
  3. Mini me.
    Devshed Novice (500 - 999 posts)

    Join Date
    Nov 2003
    Location
    Cambridge, UK
    Posts
    783
    Rep Power
    14
    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
  4. #3
  5. Hello World :)
    Devshed Frequenter (2500 - 2999 posts)

    Join Date
    Mar 2003
    Location
    Hull, UK
    Posts
    2,537
    Rep Power
    69
    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

  6. #4
  7. Hello World :)
    Devshed Frequenter (2500 - 2999 posts)

    Join Date
    Mar 2003
    Location
    Hull, UK
    Posts
    2,537
    Rep Power
    69
    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.
    programming language development: www.netytan.com Hula

  8. #5
  9. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2004
    Posts
    13
    Rep 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.

IMN logo majestic logo threadwatch logo seochat tools logo