473,385 Members | 1,893 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

write html-headers (utf-8)

db
Hello all,

I hope this is the correct newsgroup for this question.

Does anybody know how I can write a html-header with python(cgi)?
The problem is, I have a few html templates in which I have a header e.g:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">

In this template I write a few Mysql variables.
Those variable often have german characters. This characters (Gösing in
stead of Gösing). The german characters in the html template are shown
correctly.

If I change the character encoding with the browser to utf-8, all the characters are shown correctly.
As you can see, I put in the header of the html template that the encoding
is UTF-8, the browser still shows windows ISO-8859-15. Can I write the
header with python so the browser uses the utf-8 encoding?

My hosting providor uses fedora core 2, Python 2.2.3, MySQLdb. Mysql 3.23.58

I googled for hours, but I can't find the answer. I hope ypu can help me.

Thanks in advance.

Arjen

Jul 19 '05 #1
3 2593
db wrote:
In this template I write a few Mysql variables.
Those variable often have german characters. This characters (Gösing in
stead of Gösing). The german characters in the html template are shown
correctly.
The problem is then with these variables: apparently, the Mysql
variables are encoded in iso-8859-15, and you need to recode them
to UTF-8 first before putting them into the template.

var = var.decode("iso-8859-15").encode("utf-8")
As you can see, I put in the header of the html template that the encoding
is UTF-8, the browser still shows windows ISO-8859-15. Can I write the
header with python so the browser uses the utf-8 encoding?


The problem is not the header, but the body. The browser sees your claim
that the page is UTF-8, but doesn't believe it. This is because it tries
to interpret the page as UTF-8, and then finds invalid byte sequences
(your latin-9 characters), and then knows that the page *can't* be
UTF-8. It then guesses that the page must be latin-something. That guess
is wrong, of course, also, because some characters in the page are
utf-8, and others latin-9. This is invalid HTML.

Regards,
Martin

Jul 19 '05 #2
db wrote:
Hello all,

I hope this is the correct newsgroup for this question.

Does anybody know how I can write a html-header with python(cgi)?
The problem is, I have a few html templates in which I have a header e.g:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/strict.dtd"> <html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">

In this template I write a few Mysql variables.
Those variable often have german characters. This characters (Gösing in
stead of Gösing). The german characters in the html template are shown
correctly.

If I change the character encoding with the browser to utf-8, all the
characters are shown correctly. As you can see, I put in the header of the
html template that the encoding is UTF-8, the browser still shows windows
ISO-8859-15. Can I write the header with python so the browser uses the
utf-8 encoding?

My hosting providor uses fedora core 2, Python 2.2.3, MySQLdb. Mysql
3.23.58

I googled for hours, but I can't find the answer. I hope ypu can help me.

Does your browser send a Content-Type HTTP header (don't confuse this with
the HTML <head> part)? If it does and it specifies a charset, this will
override your <meta http-equiv>. Often iso-8859-1 is the default charset
for the Content-Type header.

--
Benjamin Niemann
Email: pink at odahoda dot de
WWW: http://www.odahoda.de/
Jul 19 '05 #3
db wrote:
Hello all,

I hope this is the correct newsgroup for this question.

Does anybody know how I can write a html-header with python(cgi)?
The problem is, I have a few html templates in which I have a header e.g:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">


If you are using CGI you can set the Content-Type header directly. Before you output your HTML do
print "Content-Type: text/html; charset=UTF-8"
print # blank line, end of headers

Kent
Jul 19 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: Richard Lewis | last post by:
Hi there, I'm having a problem with unicode files and ftplib (using Python 2.3.5). I've got this code: xml_source = codecs.open("foo.xml", 'w+b', "utf8") #xml_source = file("foo.xml",...
3
by: hunterb | last post by:
I have a file which has no BOM and contains mostly single byte chars. There are numerous double byte chars (Japanese) which appear throughout. I need to take the resulting Unicode and store it in a...
7
by: EmeraldShield | last post by:
We have an application that uses UTF8 everywhere to load / save / process documents. One of our clients is having a problem with BIG Encoded files being trashed after running through our app. ...
3
by: Matt | last post by:
I have a problem where I am working with extended character sets in XML but I have also found that any time I work with a translation or internally generated Xml document I get the dreaded message,...
4
by: EmeraldShield | last post by:
(Dot Net 2 C# application - using Encoding.UTF8 with a StreamReader) I have a very strange problem that I cannot explain with a UTF8 Readline() although this could exist in other types of encoding,...
7
by: amygdala | last post by:
Hi, I'm trying to let PHP write a 'sitemap.xml' sitemap for Google and other searchengines. It's working, except that the content in the XML file doesn't seem to be UTF8. (Which it should be,...
4
by: | last post by:
Hi all, I want to create a method that does the following: 1) Programmatically instantiate a new XmlDataSource control 2) For each file in a named directory, make a "FileSystemItem" element 3)...
2
by: darrel | last post by:
I was having issues before with my XML being sent to the browser as UTF16. Via my XmlTextWriter, was able to fix this by explicitely setting the encoding to UTF8: Dim objX As New...
4
by: Samuel | last post by:
Hi I am trying to write to a string text encoded to utf8 as oppose to utf16 Since the data comes from an XML object (and I serialize it) I need to pass either StreamWriter or a StringWriter...
2
by: islandgal | last post by:
-- -- Table structure for table borrower -- DROP TABLE IF EXISTS borrower; CREATE TABLE borrower ( brw_num int(11) NOT NULL default '0', brw_lname varchar(15) default NULL, brw_fname...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.