import pandas as pd
import re
# 读取Excel文件
input_excel_path = '/Users/www.macpe.cn/Desktop/ys/yuanwenjian.xlsx'
output_excel_path = '/Users/www.macpe.cn/Desktop/ys/result.xlsx'
df = pd.read_excel(input_excel_path)
# 定义正则表达式模式,匹配分号、逗号和句号
pattern = r'[,]'
# 遍历每一行,拆分正文并添加到新的行
new_rows = []
for index, row in df.iterrows():
title = row['文章标题']
content = row['正文']
sentences = re.split(pattern, content) # 使用正则表达式拆分句子
for sentence in sentences:
new_row = row.copy()
new_row['正文'] = sentence.strip()
new_rows.append(new_row)
# 创建新的DataFrame
new_df = pd.DataFrame(new_rows)
# 保存新的Excel文件
new_df.to_excel(output_excel_path, index=False)
代码片
将excel 一行分成多行
最新推荐文章于 2024-07-20 16:40:31 发布