Re: Inserting-embedding some html data at the end of a .py file

Discussion in 'Python' started by Íßêïò Ãêñ33ê, Mar 5, 2013.

  1. But i did, I just tried this:

    # open html template
    if htmlpage.endswith('.html'):
    f = open( "/home/nikos/public_html/" + htmlpage )

    htmldata = f.read()
    counter = ''' <center><a href="mailto:"> <img src="/data/images/mail.png"> </a>
    <center><table border=2 cellpadding=2 bgcolor=black>
    <td><font color=lime>Áñéèìüò Åðéóêåðôþí</td>
    <td><a href="http://superhost.gr/?show=stats"><font color=cyan> %d </td>
    ''' % data[0]
    else:
    f = open( "/home/nikos/public_html/cgi-bin/" + htmlpage )

    htmldata = f.read()
    counter = '''
    print '''<center><a href="mailto:"> <img src="/data/images/mail.png"> </a>
    <center><table border=2 cellpadding=2 bgcolor=black>
    <td><font color=lime>Áñéèìüò Åðéóêåðôþí</td>
    <td><a href="http://superhost.gr/?show=stats"><font color=cyan> %d </td>
    '''
    ''' % data[0]

    template = htmldata + counter
    print ( template )
    =============

    But still doens't embed correctly the additional html data at the end of the .py files.....

    Do you have an idea?
     
    Íßêïò Ãêñ33ê, Mar 5, 2013
    #1
    1. Advertising

  2. What extra triple quote?
    There are 2 sets of triple quotes the counter's and the print's !!

    in case htmlpage variable is a .py file i must append a print '''html''' in order for .py code to absorve that code properly no?
     
    Íßêïò Ãêñ33ê, Mar 5, 2013
    #2
    1. Advertising

  3. What extra triple quote?
    There are 2 sets of triple quotes the counter's and the print's !!

    in case htmlpage variable is a .py file i must append a print '''html''' in order for .py code to absorve that code properly no?
     
    Íßêïò Ãêñ33ê, Mar 5, 2013
    #3
  4. On 05/03/2013 17:39, Íßêïò Ãêñ33ê wrote:
    > But i did, I just tried this:
    >
    > # open html template
    > if htmlpage.endswith('.html'):
    > f = open( "/home/nikos/public_html/" + htmlpage )
    >
    > htmldata = f.read()
    > counter = ''' <center><a href="mailto:"> <img src="/data/images/mail.png"> </a>
    > <center><table border=2 cellpadding=2 bgcolor=black>
    > <td><font color=lime>Áñéèìüò Åðéóêåðôþí</td>
    > <td><a href="http://superhost.gr/?show=stats"><font color=cyan> %d </td>
    > ''' % data[0]
    > else:
    > f = open( "/home/nikos/public_html/cgi-bin/" + htmlpage )
    >
    > htmldata = f.read()
    > counter = '''
    > print '''<center><a href="mailto:"> <img src="/data/images/mail.png"> </a>
    > <center><table border=2 cellpadding=2 bgcolor=black>
    > <td><font color=lime>Áñéèìüò Åðéóêåðôþí</td>
    > <td><a href="http://superhost.gr/?show=stats"><font color=cyan> %d </td>
    > '''
    > ''' % data[0]
    >
    > template = htmldata + counter
    > print ( template )
    > =============
    >
    > But still doens't embed correctly the additional html data at the end of the .py files.....
    >
    > Do you have an idea?
    >


    You're doing something wrong.

    --
    Cheers.

    Mark Lawrence
     
    Mark Lawrence, Mar 5, 2013
    #4
  5. Íßêïò Ãêñ33ê

    Dave Angel Guest

    On 03/05/2013 12:49 PM, Îίκος ΓκÏ33κ wrote:
    > What extra triple quote?
    > There are 2 sets of triple quotes the counter's and the print's !!


    There are 3 pairs of triple-quotes. But one pair is nested inside the
    other, so the interpreter will not handle it the way you apparently
    want. If you have to use quotes inside quotes, you must either use a
    different kind, or escape them. Perhaps you wanted something like:

    counter = """
    print '''<center> etc....
    <td>font ...
    '''
    """ % data[0]

    But this fragment is unreadable to me, so the syntax error is probably
    the least of your worries.

    >
    > in case htmlpage variable is a .py file i must append a print '''html''' in order for .py code to absorve that code properly no?
    >


    No clue what that sentence fragment means.

    'absorve' isn't an English word, and neither 'absorb' nor 'observe' make
    any sense.

    Perhaps if you posted a complete program, and identified it by name, and
    specified the complete environment it runs in, and used English to
    describe what should be appended to what, we'd be able to help. As it
    is, you've got mixed clues from which that it's both Python 2 and 3,
    running Linux or equivalent, and probably runs on a server.

    Using symbol names that make sense would also help. 'htmlpage' is
    apparently a str representing a filename on disk. 'counter' is not an
    integer, but a long string of html. 'data' is apparently a tuple or
    list, and the zeroth element is an int. ??

    --
    --
    DaveA
     
    Dave Angel, Mar 5, 2013
    #5
  6. Let's focus on just the following snipper please:

    f = open( some_python_file )

    htmldata = f.read()
    counter = ''' print( "
    <center><a href="mailto:"> <img src="/data/images/mail.png"> </a>
    <center><table border=2 cellpadding=2 bgcolor=black>
    <td><font color=lime>Áñéèìüò Åðéóêåðôþí</td>
    <td><a href="http://superhost.gr/?show=stats"><font color=cyan> %d </td>
    " )
    ''' % data[0]

    #render template
    template = htmldata + counter
    print ( template )
    =============================

    What this snippet tries to accomplish is append the following string

    =========================================
    counter = ''' <center><a href="mailto:"> <img src="/data/images/mail.png"> </a>
    <center><table border=2 cellpadding=2 bgcolor=black>
    <td><font color=lime>Áñéèìüò Åðéóêåðôþí</td>
    <td><a href="http://superhost.gr/?show=stats"><font color=cyan> %d </td>
    ''' % data[0
    =========================================

    at the end of the the python script file that it currently opened.
    I'am using the print statemnt inside the triple quoted string so to append that html data by inserting a print statemnt but although i have changes the type of quoting it still fails.

    If i try to append that html data to an .html file they are inserted beautifully but inside a .py file they dont.

    I just need to append that string after the end of a .py file. that's all iwant to do.
     
    Íßêïò Ãêñ33ê, Mar 5, 2013
    #6
  7. Let's focus on just the following snipper please:

    f = open( some_python_file )

    htmldata = f.read()
    counter = ''' print( "
    <center><a href="mailto:"> <img src="/data/images/mail.png"> </a>
    <center><table border=2 cellpadding=2 bgcolor=black>
    <td><font color=lime>Áñéèìüò Åðéóêåðôþí</td>
    <td><a href="http://superhost.gr/?show=stats"><font color=cyan> %d </td>
    " )
    ''' % data[0]

    #render template
    template = htmldata + counter
    print ( template )
    =============================

    What this snippet tries to accomplish is append the following string

    =========================================
    counter = ''' <center><a href="mailto:"> <img src="/data/images/mail.png"> </a>
    <center><table border=2 cellpadding=2 bgcolor=black>
    <td><font color=lime>Áñéèìüò Åðéóêåðôþí</td>
    <td><a href="http://superhost.gr/?show=stats"><font color=cyan> %d </td>
    ''' % data[0
    =========================================

    at the end of the the python script file that it currently opened.
    I'am using the print statemnt inside the triple quoted string so to append that html data by inserting a print statemnt but although i have changes the type of quoting it still fails.

    If i try to append that html data to an .html file they are inserted beautifully but inside a .py file they dont.

    I just need to append that string after the end of a .py file. that's all iwant to do.
     
    Íßêïò Ãêñ33ê, Mar 5, 2013
    #7
  8. On Tue, Mar 5, 2013 at 1:53 PM, Îίκος ΓκÏ33κ <> wrote:

    > Let's focus on just the following snipper please:
    >
    > f = open( some_python_file )
    >
    > htmldata = f.read()
    > counter = ''' print( "
    > <center><a href="mailto:support@superhost..gr">
    > <img src="/data/images/mail.png"> </a>
    > <center><table border=2 cellpadding=2
    > bgcolor=black>
    > <td><font color=lime>ΑÏιθμός
    > Επισκεπτών</td>
    > <td><a href="
    > http://superhost.gr/?show=stats"><font color=cyan> %d </td>
    > " )
    > ''' % data[0]
    >
    > #render template
    > template = htmldata + counter
    > print ( template )
    > =============================
    >
    > What this snippet tries to accomplish is append the following string
    >
    > =========================================
    > counter = ''' <center><a href="mailto:">
    > <img src="/data/images/mail.png"> </a>
    > <center><table border=2 cellpadding=2
    > bgcolor=black>
    > <td><font color=lime>ΑÏιθμός
    > Επισκεπτών</td>
    > <td><a href="
    > http://superhost.gr/?show=stats"><font color=cyan> %d </td>
    > ''' % data[0
    > =========================================
    >
    > at the end of the the python script file that it currently opened.
    > I'am using the print statemnt inside the triple quoted string so to append
    > that html data by inserting a print statemnt but although i have changes
    > the type of quoting it still fails.
    >
    > If i try to append that html data to an .html file they are inserted
    > beautifully but inside a .py file they dont.
    >
    > I just need to append that string after the end of a .py file. that's all
    > i want to do.
    > --
    > http://mail.python.org/mailman/listinfo/python-list
    >


    print displays to the console. To write to a file open file in 'append'
    mode and write:

    with open("test.py", "a") as myfile:
    myfile.write("appended text")



    --
    Joel Goldstick
    http://joelgoldstick.com
     
    Joel Goldstick, Mar 5, 2013
    #8
  9. Íßêïò Ãêñ33ê

    Dave Angel Guest

    On 03/05/2013 01:53 PM, Íßêïò Ãêñ33ê wrote:
    > Let's focus on just the following snipper please:


    Once again, I repeat. Make a fragment that contains enough information
    to actually run. Explain in what environment it's running, and what you
    hoped would happen. For example, why on earth would you assume that
    printing to the console below would write to a file instead? Is some
    part of this running as a cgi inside a web server? What is your
    environment?
    >
    > f = open( some_python_file )
    >
    > htmldata = f.read()
    > counter = ''' print( "


    Syntax error here. Since the inner string is more than one line, you
    will need to use """. See my example last message. This only matters
    if you succeed in writing this to a python file.

    > <center><a href="mailto:"> <img src="/data/images/mail.png"> </a>
    > <center><table border=2 cellpadding=2 bgcolor=black>
    > <td><font color=lime>Áñéèìüò Åðéóêåðôþí</td>
    > <td><a href="http://superhost.gr/?show=stats"><font color=cyan> %d </td>
    > " )
    > ''' % data[0]
    >
    > #render template
    > template = htmldata + counter
    > print ( template )


    Why should print write to some python file you have open?? Besides you
    have the file open for readonly access. So you might use:

    f.close()
    f = open( some_python_file, "w" )
    f.write(template)
    f.close()


    > =============================
    >
    > What this snippet tries to accomplish is append the following string
    >
    > =========================================
    > counter = ''' <center><a href="mailto:"> <img src="/data/images/mail.png"> </a>
    > <center><table border=2 cellpadding=2 bgcolor=black>
    > <td><font color=lime>Áñéèìüò Åðéóêåðôþí</td>
    > <td><a href="http://superhost.gr/?show=stats"><font color=cyan> %d </td>
    > ''' % data[0
    > =========================================
    >
    > at the end of the the python script file that it currently opened.


    But that's not at all what's inside the counter variable.

    > I'am using the print statemnt inside the triple quoted string so to append that html data by inserting a print statemnt but although i have changes the type of quoting it still fails.


    Don't ever say "it fails". If you can't be bothered to explain in what
    way it fails, forget it. Perhaps you're trying to say it prints a
    string to the console instead of writing a different string to the
    /fullpath/to/some_python_file.py

    Or perhaps you're saying it crashed the Windows machine, and killed
    power for miles around. Or perhaps you're saying it got an exception
    but we won't bother telling you which.


    >
    > If i try to append that html data to an .html file they are inserted beautifully


    Untrue, unless your environment is specified, it certainly did not.
    perhaps you were running the program with output redirected to a file,
    in which case it lost the earlier version. Or perhaps you were running
    it on a web server, and you *think* some file changed because some
    obscure thing happened on your browser. What's your environment?

    > but inside a .py file they dont.
    >
    > I just need to append that string after the end of a .py file. that's all i want to do.
    >


    Easy to do in emacs.


    --
    DaveA
     
    Dave Angel, Mar 5, 2013
    #9
  10. Hi,

    On Tue, Mar 05, 2013 at 09:39:19AM -0800, Îίκος ΓκÏ33κ wrote:
    > But i did, I just tried this:
    >
    > # open html template
    > if htmlpage.endswith('.html'):
    > f = open( "/home/nikos/public_html/" + htmlpage )
    >
    > htmldata = f.read()
    > counter = ''' <center><a href="mailto:"> <img src="/data/images/mail.png"> </a>
    > <center><table border=2 cellpadding=2 bgcolor=black>
    > <td><font color=lime>ΑÏιθμός Επισκεπτών</td>
    > <td><a href="http://superhost.gr/?show=stats"><font color=cyan> %d </td>
    > ''' % data[0]
    > else:
    > f = open( "/home/nikos/public_html/cgi-bin/" + htmlpage )
    >
    > htmldata = f.read()
    > counter = '''
    > print '''<center><a href="mailto:"> <img src="/data/images/mail.png"> </a>
    > <center><table border=2 cellpadding=2 bgcolor=black>
    > <td><font color=lime>ΑÏιθμός Επισκεπτών</td>
    > <td><a href="http://superhost.gr/?show=stats"><font color=cyan> %d </td>
    > '''
    > ''' % data[0]
    >
    > template = htmldata + counter
    > print ( template )
    > =============
    >
    > But still doens't embed correctly the additional html data at the end of the .py files.....
    >
    > Do you have an idea?

    as someone said: You're doing it the wrong way.

    I would recommend to use a template-engine; then you can put the
    complete html-design (and some design-control-structures) into
    the template (and *not* into the cgi) and fill data into the
    template with a python-script.


    Roland
     
    Roland Koebler, Mar 6, 2013
    #10
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Jimmy
    Replies:
    3
    Views:
    341
    J. J. Farrell
    Sep 9, 2011
  2. Renie83
    Replies:
    1
    Views:
    189
    Kiran Math
    Jul 10, 2003
  3. Íßêïò Ãêñ33ê
    Replies:
    2
    Views:
    158
    Joel Goldstick
    Mar 5, 2013
  4. Íßêïò Ãêñ33ê
    Replies:
    4
    Views:
    160
    Íßêïò Ãêñ33ê
    Mar 5, 2013
  5. Íßêïò Ãêñ33ê
    Replies:
    34
    Views:
    513
    Chris Angelico
    Mar 7, 2013
Loading...

Share This Page