Python 提取 Excel 酒店评论数据并生成 TXT 文件
import pandas as pd
读取excel文件
df = pd.read_excel('hotel_comments.xlsx')
获取空行的行数
empty_rows = df[df.isnull().all(axis=1)].index.tolist()
添加文件夹路径
folder_path = 'hotels_comments/'
遍历空行行数,将每个酒店的评论写入文本文件
for i in range(len(empty_rows)-1): # 获取酒店名称 hotel_name = df.iloc[empty_rows[i], 0] # 获取评论内容 comments = df.iloc[empty_rows[i]+1:empty_rows[i+1], 3:5].dropna(how='all').values.tolist() # 写入txt文件 with open(folder_path + hotel_name + '.txt', 'w') as f: for c in comments: f.write(' '.join(c) + ' ')
原文地址: https://www.cveoy.top/t/topic/ooOw 著作权归作者所有。请勿转载和采集!