Python Forum
replace html string with python variable !
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
replace html string with python variable !
#1
hello all ...
im trying to run this command on the system ( whoami ) and i need to result for the command to be saved in html page ...

this is my code ...

import os
qassam = os.system("whoami")

html_str = """
<!DOCTYPE html>
<html>
<head>
<title>Page Title</title>
</head>
<body>

<h1>test</h1> <============================================== i need to replace test with ( qassam = os.system("whoami") ) result !!
<p>My first paragraph.</p>

</body>
</html>
"""

Html_file= open(r"C:\Users\adam\AppData\q.html","w")
Html_file.write(html_str)
Html_file.close()
how i can do that ?
Reply
#2
better use popen

import os

process = os.popen('whoami')
myname = process.read()
process.close()
 
html_str = """
<!DOCTYPE html>
<html>
<head>
<title>Page Title</title>
</head>
<body>
 
<h1>test</h1>
<p>My first paragraph.</p>
 
</body>
</html>
"""
 
html_str = html_str.replace("test", myname)

Html_file= open(r"/tmp/q.html","w")
Html_file.write(html_str)
Html_file.close()
Reply
#3
(Aug-30-2018, 11:12 AM)Axel_Erfurt Wrote: better use popen

import os

process = os.popen('whoami')
myname = process.read()
process.close()
 
html_str = """
<!DOCTYPE html>
<html>
<head>
<title>Page Title</title>
</head>
<body>
 
<h1>test</h1>
<p>My first paragraph.</p>
 
</body>
</html>
"""
 
html_str = html_str.replace("test", myname)

Html_file= open(r"/tmp/q.html","w")
Html_file.write(html_str)
Html_file.close()

works thank u :)
can u explain why u used myname = process.read() ?
Reply
#4
to keep the result of popen for using it for replace in the html string.

you can name it whatever you want.
Reply
#5
Can also show a example with Jinja2.
Jinja2 is build for stuff like this,it's mostly used with a running server to transport values to HTML on client side.
It also work fine on it's own.
from jinja2 import Environment, FileSystemLoader
import os

html_str = """\
<!DOCTYPE html>
<html>
<head>
<title>Page Title</title>
</head>
<body>

<h1>{{ test }}</h1>
<p>My first paragraph.</p>

</body>
</html>"""

process = os.popen('whoami')
myname = process.read().strip()
process.close()

# Print html
print(Environment().from_string(html_str).render(test=myname))

# Save html
#Environment().from_string(html_str).stream(test=myname).dump('name.html', encoding='utf-8')
Output:
<!DOCTYPE html> <html> <head> <title>Page Title</title> </head> <body> <h1>Pc User Name</h1> <p>My first paragraph.</p> </body> </html>
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Replace a text/word in docx file using Python Devan 4 3,290 Oct-17-2023, 06:03 PM
Last Post: Devan
  Need to replace a string with a file (HTML file) tester_V 1 760 Aug-30-2023, 03:42 AM
Last Post: Larz60+
  Replacing String Variable with a new String Name kevv11 2 772 Jul-29-2023, 12:03 PM
Last Post: snippsat
  why doesn't it replace all html tags? Melcu54 3 739 Jul-05-2023, 04:47 AM
Last Post: Melcu54
  Replace string in a nested Dictianory. SpongeB0B 2 1,191 Mar-24-2023, 05:09 PM
Last Post: SpongeB0B
  Replace with upper(string) WJSwan 7 1,582 Feb-10-2023, 10:28 AM
Last Post: WJSwan
  Need help on how to include single quotes on data of variable string hani_hms 5 2,012 Jan-10-2023, 11:26 AM
Last Post: codinglearner
  Tkinterweb (Browser Module) Appending/Adding Additional HTML to a HTML Table Row AaronCatolico1 0 921 Dec-25-2022, 06:28 PM
Last Post: AaronCatolico1
  python r string for variable mg24 3 2,788 Oct-28-2022, 04:19 AM
Last Post: deanhystad
  USE string data as a variable NAME rokorps 1 958 Sep-30-2022, 01:08 PM
Last Post: deanhystad

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020