1

I was trying to convert a csv data to html using Pandas. But i was getting the below error UnicodeEncodeError('ascii', u'', 10305, 10306, 'ordinal not in range(128)')

How to resolve this issue

1
  • 2
    Please share your inputs and outputs of code Commented Apr 3, 2018 at 13:54

1 Answer 1

1

Importing the pandas module:

import pandas as pd

I create an example csv-file, "odd_numbers.csv", containing the odd numbers from 1 to 9:

1,3,5,7,9 

First convert your CSV file (here "odd_numbers.csv") into a Pandas Dataframe, "df":

filename      = "odd_numbers.csv"
df            = pd.read_csv(filename, header = None)

Then use Pandas to_html function on your new dataframe, "df", and make sure to set the "classes" argument for this function equal to "utf8":

HTML_file    = df.to_html(classes='utf8') 

The HTML file, "HTML_file", will then contain the following:

<table border="1" class="dataframe utf8">
  <thead>
    <tr style="text-align: right;">
      <th></th>
      <th>0</th>
      <th>1</th>
      <th>2</th>
      <th>3</th>
      <th>4</th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <th>0</th>
      <td>1</td>
      <td>3</td>
      <td>5</td>
      <td>7</td>
      <td>9</td>
    </tr>
  </tbody>
</table>
Sign up to request clarification or add additional context in comments.

3 Comments

At df.to_html step am facing the issue of "UnicodeEncodeError('ascii', u'', 10305, 10306, 'ordinal not in range(128)')"
Set your 'classes' argument, inside Pandas to_html method, to 'utf8'. In the above example: HTML_file = df.to_html(classes='utf8')
When am trying to push the HTML data (df.to_html(classes='utf8') ) into a .html file still facing the same issue UnicodeEncodeError

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.