Python 将html数据提取到Excel电子表格
我正在尝试使用python“读取”html文档,并将输出写入excel电子表格。HTML文件是CU(成本单位,由所有CAP定义)和说明的表格。我想在一列中列出CU,在另一列中列出相应的描述。我有一个全局存储部分文本,直到它到达CU,然后将文本放入正确的列中,但由于某些原因,代码无法完成所有CU的列表,并且它无法将描述放置在正确的位置(将它们从适用CU向下放置一列)。有人能帮我找出我做错了什么吗?以下是我目前的代码:Python 将html数据提取到Excel电子表格,python,html,excel,Python,Html,Excel,我正在尝试使用python“读取”html文档,并将输出写入excel电子表格。HTML文件是CU(成本单位,由所有CAP定义)和说明的表格。我想在一列中列出CU,在另一列中列出相应的描述。我有一个全局存储部分文本,直到它到达CU,然后将文本放入正确的列中,但由于某些原因,代码无法完成所有CU的列表,并且它无法将描述放置在正确的位置(将它们从适用CU向下放置一列)。有人能帮我找出我做错了什么吗?以下是我目前的代码: from HTMLParser import HTMLParser import
from HTMLParser import HTMLParser
import xlwt
global wb
global ws
global cucounter
global textcounter
global tempcu
textstore = ""
cucounter = 0
textcounter = 0
wb = xlwt.Workbook()
ws = wb.add_sheet('A Test Sheet')
filename = 'C:\\Python27\\ArcGIS10.3\\Doc\\Page.html'
f = open(filename, "r").read()
class MyHTMLParser(HTMLParser):
def handle_data(self, data):
if data.isupper():
try:
global cucounter
ws.write(cucounter, 1, data)
cucounter = cucounter + 1
wb.save('ElecTest.xls')
except UnicodeDecodeError:
pass
if data.isspace():
pass
else:
try:
global textstore
textstore += str(data)
if data.isupper():
global textstore
global textcounter
ws.write(textcounter, 2, textstore)
textcounter = textcounter + 1
textstore = ""
wb.save('ElectTest.xls')
except UnicodeDecodeError:
pass
parser = MyHTMLParser()
parser.feed(f)
不幸的是,我无法以正确的格式添加HTML文件(如果可以的话,UnicodeDecodeError处理会有意义),但我可以复制以下内容:
页面C/U说明:
M-M
您能发布部分html以获得可复制的示例吗?我将word文档导出为html,以便用python读取它(我只能使用内置模块)。HTML在word中看起来没有什么不同。为了查看HTML,我应该以什么方式打开它?右键单击它并使用任何web浏览器打开。一旦呈现出来,请转到页面的源代码并发布HTML标记。@Parfait我根据您的请求在顶部添加了几百行,请告诉我您还需要什么。请发布文件的开头,而不是文档中间的行。我们需要声明头和一些行来帮助理解模式。另外,为什么不在Excel中的“打开选择html文档”下在电子表格中打开它?Excel会将内容映射到单元格。
EULBPIT Excavate, backfill & tamp auger pit or primary splice hole. Qty "1" per occurrence. 4'X4'X5' pit.
EULBCOMPWHEEL Wheel Compaction - Tamping with wheel, where initial lift is rolled, trench filled & crowned and rolled again and where additional traffic is expected in location assists with tamping.
EULBCOMP85STD 85% Std. Proctor Compaction - Trench where subsidence is unsettled and probable due to nature of area, needing compaction equipment w/ 12” lifts, use in parking lots, adjacent to roadways & front lot line URD.
EULBCOMP85MOD 85% Modified Proctor Compaction - Trenches under hard surfaces of roadway, more rigid than std, requiring compaction equipment w/ maximum 12” lifts, minimum12” lift from cable, soil and moisture content critical, hand test required also.
EULBCOMP95STD 95% Std. Proctor Compaction - Used by most local jurisdictions, close to, but more than, 85% but needing more moisture, 12” lifts should be used and hand test for adequate moisture.
EULBCOMP95MOD 95% Modified Proctor Compaction - Trenches under hard surfaces of roadway, more rigid than std, requiring compaction equipment w/ maximum 12” lifts, minimum12” lift from cable, soil and moisture content critical, hand test required.
EULBCOMP Compaction Test
EULBSHORE Shoring, 5’ high, 2-sided per ft per day
EULBTHAWU Thaw master/UG work: Specify "1" in install column only. Includes install, remove, lighting, & setting (2) burners with propane tank.
EULBJACKHAMMER Jackhammer: Specify per sq ft X 4" deep. Install column only.
EULBHANDIKRETE Handikrete. Install only-Specify "1" per cu ft (1-bag).
EUCDJACK4STPIPE Jack 4" galvanized steel pipe - includes pipe & coupling. Set up and dismantle jacking equipment. Specify "1" per ft.
EUCDJACK5STPIPE Jack 5" galvanized steel pipe - includes pipe & coupling. Set up and dismantle jacking equipment. Specify "1" per ft.
EUCDJACK6STPIPE Jack 6" galvanized steel pipe - includes pipe & coupling. Set up and dismantle jacking equipment. Specify "1" per ft.
EUCDIN-OUTJACK Setting up & dismantle jacking equipment. Includes digging & filling of pits. Specify "1" per occurrence in the install column only.
EUCDCASE24 Jack 24" casing--Specify "1" per ft - does not include pipe.
EULBYSNOW Snow removal. Install column only; specify “1” for every 2 man-hours.
EULBCLEANADJUST Clean or adjust switchgear. Install only; specify “1” per occurrence.
EULBUGLC Install or remove line covers. Specify # of covers and occurrences.
EULBLOWRCBL Lowering cable - specify per linear ft. Install column only.
EULBGROUNDCBL Install, remove or test for ground on cable. Specify “1” per occurrence.
EULBMOVECBL Place terminator on stand-off or energized bushing. Specify “1” per occurrence. Install column only.
EULBPHASE-U Phase-in UG conductor. Install only; specify “1” per occurrence.
EULBTRANSRISER Transfer riser cable. Specify # of cables; install only: specify “1” per occurrence.
EULBLOCATEFAULT Find UG cable fault - Install column only; specify “1” per occurrence.
EULBCBLIDTESTER Identify cable with impulse phaser. Specify “1” per occurrence
EULBPIERCECBL Ground pierce cable - Install column only. Specify “1” per occurrence.
EULBSWITCH Switch URD 600 A PMH gear. Specify “1” per occurrence.
EULBSWOIL Switch-open & close OCR & Leads. Specify “1” per occurrence.
EULBPDLCK Padlock open and close. Specify “1” per occurrence.
EULBCOVERHOLE Plywood to cover construction hole. Specify “1’ per occurrence.
EULBSCRTYFENCNG Remove/replace/install security fencing (orange) around splice pit. Specify “1” per occurrence.
EULBDRTPKUP Dirt pick-up: Load & haul excess dirt on site, per cu yd.
EULBDRTPKPD Dirt pick-up: Load & haul excess dirt off site, per cu yd.
EULBROADBASE Road base, labor only to install; specify "1" per cu yd.