python导入导出excel、python提取html正文

需求是处理一个excel表格
表格结构为:标题、正文的xml文件地址
结果表:标题、处理了xml、html标签后的正文

总代码

import pandas as pd
import xml.etree.ElementTree as ET
import re

if __name__ == '__main__':
    baseUrl = "/Desktop/工作项目/AI/工作-语料/**信息/"
    # 读取Excel文件
    excel_data = pd.read_excel(baseUrl + '**信息.xlsx')

    # 创建一个空的列表,用于存储每行的数据
    rows_data = []

    # 错误计数
    cnt = 0
    # 总行数
    index = 0

    # 遍历Excel文件的每一行
    for index, row in excel_data.iterrows():
        # 读取XML文件路径
        xml_path = row['文章正文']
        try:

            print(index)
            # 判断XML文件路径是否为空
            if pd.isnull(xml_path):
                print('文章正文为空:', row['标题'])
                cnt += 1
                continue

            # 打开XML文件
            with open(baseUrl + 'info/' + xml_path, 'r', encoding='utf-8') as file:
                html_content = file.read()
                tree = ET.fromstring(html_content)

                # 提取<content>标签中的内容
                content_start = html_content.find('<content>') + len('<content>')
                content_end = html_content.find('</content>')
                content_text = html_content[content_start:content_end]

                # 去除HTML标签
                cleaned_text = re.sub('<[^<]+?>', '', content_text)
                # 删除所有的&nbsp;
                cleaned_text = cleaned_text.replace('&nbsp;', '')
                # 判断文章正文是否为空
                if not cleaned_text:
                    print('文章正文为空:', row['标题'])
                    cnt += 1
                    continue
                # 判断正文长度是否小于100
                if len(cleaned_text) < 100:
                    print('文章正文长度小于100:', row['标题'])
                    print('文章正文:', cleaned_text)
                    cnt += 1
                    continue

                # 添加到列表中
                rows_data.append({'标题': row['标题'], '文章正文': cleaned_text})
        except Exception as e:
            print('-----------------------error:', row['标题'])
            print('Exception type:', type(e).__name__)
            print('Error message:', e)

    # 创建新的DataFrame
    new_data = pd.DataFrame(rows_data)
    print(cnt)

    # 导出到Excel文件
    new_data.to_excel(baseUrl + 'new_file.xlsx', index=False)

其中

1. excel导入、导出

使用pandas,例子:


import pandas as pd

# 读取Excel文件
excel_data = pd.read_excel('file.xlsx')

# 打印数据
print(excel_data)

遍历每一行:

for index, row in excel_data.iterrows():

取出某一列的所有行:

import pandas as pd

# 读取Excel文件
excel_data = pd.read_excel('file.xlsx')

# 读取特定列数据,比如第一列
column_data = excel_data['ColumnName']

导出数据:

    rows_data = [{'标题': '标题', '文章正文': '文章正文'}]
    # 创建新的DataFrame
    new_data = pd.DataFrame(rows_data)

    # 导出到Excel文件
    new_data.to_excel(baseUrl + 'new_file.xlsx', index=False)
2. python打开本地文件
            with open(baseUrl + 'info/' + xml_path, 'r', encoding='utf-8') as file:
                html_content = file.read()
3. 抽取数据并去掉标签
# 提取<content>标签中的内容
                content_start = html_content.find('<content>') + len('<content>')
                content_end = html_content.find('</content>')
                content_text = html_content[content_start:content_end]

                # 去除HTML标签
                cleaned_text = re.sub('<[^<]+?>', '', content_text)
                # 删除所有的&nbsp;
                cleaned_text = cleaned_text.replace('&nbsp;', '')

相关推荐

  1. python导入导出excel、python提取html正文

    2024-03-28 09:14:04       21 阅读
  2. pythonhtml正文提取(CEPF)

    2024-03-28 09:14:04       6 阅读
  3. MongoDB导入导出命令

    2024-03-28 09:14:04       31 阅读
  4. Oracle导出导入dmp

    2024-03-28 09:14:04       29 阅读
  5. Oracle导入导出dump

    2024-03-28 09:14:04       38 阅读

最近更新

  1. TCP协议是安全的吗?

    2024-03-28 09:14:04       16 阅读
  2. 阿里云服务器执行yum,一直下载docker-ce-stable失败

    2024-03-28 09:14:04       16 阅读
  3. 【Python教程】压缩PDF文件大小

    2024-03-28 09:14:04       15 阅读
  4. 通过文章id递归查询所有评论(xml)

    2024-03-28 09:14:04       18 阅读

热门阅读

  1. ES6 常用知识点英文单词总结

    2024-03-28 09:14:04       18 阅读
  2. npm 的常见指令及用法

    2024-03-28 09:14:04       18 阅读
  3. macOS安装mongoDB(homebrew)

    2024-03-28 09:14:04       21 阅读
  4. kafka

    kafka

    2024-03-28 09:14:04      17 阅读
  5. kafka-eagle 配置文件修改使用自带的数据库

    2024-03-28 09:14:04       19 阅读
  6. Mac电脑怎么配置ohpm的环境变量

    2024-03-28 09:14:04       20 阅读
  7. Day59| 503 下一个更大元素II 42 接雨水

    2024-03-28 09:14:04       21 阅读
  8. 用html画一个烟花特效

    2024-03-28 09:14:04       13 阅读