赞
踩
最近接到一批及其不规范的xls文件,本质是html格式,只是修改后缀为xls而已,因为有大量数字字符串,导致大量数据用Excel打开时,读取的数据失真,不得已自己写了个读取程序。全部读取后,默认为文本字符串,转换为标准Excel文件。
# -*- coding: utf-8 -*-
"""
Created on Wed Apr 17 22:16:03 2019
@author: user
"""
import bs4
#import requests
from pandas.core.frame import DataFrame
def read_file(path):
with open(path, 'r+',
encoding='UTF-8') as f:
str = f.read()
return
str.strip().replace('\ufeff', '')
bgne_lb=[]#设置一个列表用于接收数据
html=r'C:\Users\user\Desktop\测试.htm'#假设这个就是文件在本地的地址
wb=read_file(html)
soup=bs4.BeautifulSoup(wb,'lxml')#解析html
#elems=soup.findAll("table",{"class":"wikitable"})[0]#当需要进一步精确匹配时才使用
table_ys=soup.findAll("table")[0]#读取第一个表格
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。