Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
This script reads a text file containing a HTML table, uses pandas to parse that table into a dataframe. Then writes that dataframe as a CSV to another text file.

import pandas as pd

html =
df = pd.read_html(html)
Problem is some of the cells of the table randomly contain URL encoded HTML. So for example <h1>My Heading</h1> will be %3Ch1%3EMy%20Heading%3C%2Fh1%3E

Ive tried

import pandas as pd
import urllib

html =
df = pd.read_html(html)
But that results in scrambled results in the CSV.
Have to be careful when downloading HTML to disk,and then try to read it back again so encoding don't get mess up.
Should always use Requests,then get correct encoding back.
>>> import requests
>>> response = requests.get('')
>>> response.status_code
>>> response.encoding
To disk:
import requests

response = requests.get('')
html = response.text
with open('html_raw.html', 'w', encoding='ISO-8859-1') as f_out:
Read saved data pandas:
[Image: rZ7nNw.jpg]
I could of course also read the url,without saving to disk.
df = pd.read_html('', header=0)
Sorry I am a learner. What I want to do is convert data inside dataframe. So that



<h1>My Heading</h1>
(Jan-02-2019, 01:36 AM)UnionSystems Wrote: Sorry I am a learner. What I want to do is convert data inside dataframe. So that
What i mean is that HTML can be messed up by our saving before get into pandas DataFrame.
I can not tell how you get that html data to disk.
Just decode,answers will always by in Python 3(as you also should use).
>>> import urllib.parse
>>> url = '%3Ch1%3EMy%20Heading%3C%2Fh1%3E'
>>> urllib.parse.unquote(url)
'<h1>My Heading</h1>'
I cannot change the data I am getting from the text file.

So how can I

to all rows in a dataframe.

I have tried

df=df.apply(lambda x:urllib.unquote(x).decode('utf8'))
But get error

AttributeError: ("'Series' object has no attribute 'split'", u'occurred at index 0')
Found the solution Big Grin with only 2 lines of code

import pandas as pd
import urllib
html =
df = pd.read_html(html)
df = df[0].applymap(str)
df = df.applymap(urllib.unquote_plus)

Possibly Related Threads…
Thread Author Replies Views Last Post
  flask app decoding problem mesbah 0 1,466 Aug-01-2021, 08:32 PM
Last Post: mesbah
  Decoding a serial stream AKGentile1963 7 4,860 Mar-20-2021, 08:07 PM
Last Post: deanhystad
  xml decoding failure(bs4) roughstroke 1 1,502 May-09-2020, 04:37 PM
Last Post: snippsat
  python3 decoding problem but python2 OK mesbah 0 1,336 Nov-30-2019, 04:42 PM
Last Post: mesbah
  utf-8 decoding failed every time i try adnanahsan 21 7,427 Aug-27-2019, 04:25 PM
Last Post: adnanahsan
  hex decoding in Python 3 rdirksen 2 3,794 May-12-2019, 11:49 AM
Last Post: rdirksen
  Decoding log files in binary using an XML file. captainfantastic 1 1,818 Apr-04-2019, 02:24 AM
Last Post: captainfantastic
  decoding sub.process output with multiple \n? searching1 2 2,121 Feb-24-2019, 12:00 AM
Last Post: searching1
  Decoding Hex / understanding Code NoWay 4 3,224 Mar-20-2018, 02:48 PM
Last Post: NoWay
  base64 decoding issue or bug rbrahmaa 2 11,729 Apr-25-2017, 11:56 PM
Last Post: wavic

Forum Jump:

User Panel Messages

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