email.header: Internationalized headers,The email.header module also provides the following convenient functions.,Create a Header instance from a sequence of pairs as returned by decode_header().,Source code: Lib/email/header.py
>>> from email.message
import Message
>>>
from email.header
import Header
>>>
msg = Message() >>>
h = Header('p\xf6stal', 'iso-8859-1') >>>
msg['Subject'] = h >>>
msg.as_string()
'Subject: =?iso-8859-1?q?p=F6stal?=\n\n'
>>> from email.header
import decode_header
>>>
decode_header('=?iso-8859-1?q?p=F6stal?=')[(b 'p\xf6stal', 'iso-8859-1')]
In your header.py
import module1
import module2
In your other files,
import header
header.module1.method() #make all the
function calls via header module.
If you think its making your code writing tedious because of longer function names, then may be try this.
import header as h
h.module1.method()
In the first example, we use pyquery module to get the text of a header. , We import the PyQuery class from the pyquery module. The PyQuery is the main class for doing work. , We open the index.html file and read its contents with the read method. , We select the first li tag and print its content with the text method.
Pyquery is installed with the following command:
$ sudo pip3 install pyquery
<!DOCTYPE html>
<html>
<head>
<title>Header</title>
<meta charset="utf-8">
</head>
<body>
<h2>Operating systems</h2>
<ul id="mylist" style="width:150px">
<li>Solaris</li>
<li>FreeBSD</li>
<li>Debian</li>
<li>NetBSD</li>
<li>Windows</li>
</ul>
</body>
</html>
#!/usr/bin/python
from pyquery
import PyQuery as pq
with open("index.html", "r") as f:
contents = f.read()
doc = pq(contents)
text = doc("h2").text()
print(text)
We import the PyQuery
class from the pyquery
module. The PyQuery
is the main class for doing work.
with open("index.html", "r") as f:
contents = f.read()
We open the index.html
file and read its contents
with the read
method.
doc = pq(contents)