How to use UTF-8 encoding to export DATAFRAME to HTML?

I have been:

UnicodeEncodeError:'ascii' codec can't encode characters in position 265-266: ordinal not in range(128)

When I try:

df.to_html("mypage.html")

This is An example of how to reproduce the problem:

df = pd.DataFrame({"a": [u'Rue du Gué, 78120 Sonchamp'], " b": [u"some other thing"]})
df.to_html("mypage.html")

The element list in “a” is of type “unicode”.

When I want to export it to csv it is valid because you can do this:

df.to_csv("myfile.csv", encoding=" utf-8")

Your problem lies in other code. Your sample code has Unicode String, the string was incorrectly decoded as latin1, Windows-1252 or similar because it contains a UTF-8 sequence. Here I removed the wrong decoding and re-encoded to UTF-8, but you want to find Where to perform wrong decoding:

>>> s = u'Rue du Gué, 78120 Sonchamp'
>>> s. encode('latin1').decode('utf8')
u'Rue du Gué, 78120 Sonchamp'
>>> print(s.encode('latin1').decode('utf8 '))
Rue du Gué, 78120 Sonchamp

I have been here:

UnicodeEnc odeError:'ascii' codec can't encode characters in position 265-266: ordinal not in range(128)

When I try:

df .to_html("mypage.html")

This is an example of how to reproduce the problem:

df = pd.DataFrame({"a" : [u'Rue du Gué, 78120 Sonchamp'], "b": [u"some other thing"]})
df.to_html("mypage.html")

< p>The list of elements in “a” is of type “unicode”.

When I want to export it to csv it is valid, because you can do this:

p>

df.to_csv("myfile.csv", encoding="utf-8")

Your problem lies in other code. Your sample code has a Unicode string that was incorrectly decoded as latin1, Windows-1252 or similar because it contains a UTF-8 sequence. Here I removed the wrong decoding and re-encoded to UTF-8 , But you want to find the place to perform error decoding:

>>> s = u'Rue du Gué, 78120 Sonchamp'
>>> s.encode('latin1').decode('utf8')
u'Rue du Gué, 78120 Sonchamp'
>>> print(s.encode('latin1' ).decode('utf8'))
Rue du Gué, 78120 Sonchamp

WordPress database error: [Table 'yf99682.wp_s6mz6tyggq_comments' doesn't exist]
SELECT SQL_CALC_FOUND_ROWS wp_s6mz6tyggq_comments.comment_ID FROM wp_s6mz6tyggq_comments WHERE ( comment_approved = '1' ) AND comment_post_ID = 4956 ORDER BY wp_s6mz6tyggq_comments.comment_date_gmt ASC, wp_s6mz6tyggq_comments.comment_ID ASC

Leave a Comment

Your email address will not be published.