import pandas as pd import os

读取Excel文件

df = pd.read_excel('hotel_comments.xlsx')

创建存储文本文件的文件夹

if not os.path.exists('hotels_comments'): os.mkdir('hotels_comments')

遍历每一行

hotel_name = '' for index, row in df.iterrows(): if pd.notnull(row['酒店名称']): # 如果该行有酒店名称 hotel_name = row['酒店名称'] # 创建酒店名称对应的txt文件 with open(f'hotels_comments/{hotel_name}.txt', 'w', encoding='utf-8') as f: pass if pd.notnull(row['好的方面']): # 如果该行有好的方面 # 将好的方面写入txt文件 with open(f'hotels_comments/{hotel_name}.txt', 'a', encoding='utf-8') as f: f.write(row['好的方面'] + ' ') if pd.notnull(row['差的方面']): # 如果该行有差的方面 # 将差的方面写入txt文件 with open(f'hotels_comments/{hotel_name}.txt', 'a', encoding='utf-8') as f: f.write(row['差的方面'] + ' '

有一个excel文件标题是酒店名称客户整体评价入住时间好的方面差的方面五列文件内容是多家酒店的客户评论一个酒店的评论写在了一起不同的酒店之间空了一行需要读取这个excel文件将每个酒店的评论内容里面的好的方面差的方面这两列单独写入txt文本文件txt文本不要表头文件名是酒店名称;所有酒店的文本文件放在名字为hotels_comments这个文件夹下怎么写python代码?修改你写的:import

原文地址: https://www.cveoy.top/t/topic/fJbe 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录