一、整理一个包含刷单信息的excel
excel的名字:excel_comment.xlsx
excel的列名:买家会员名收货人金额评论
二、生成好的评论
注意:两边的数据条数必须一样,比如:有5个刷单人,就必须有5个评价!!!
三、代码
import pandas as pd
import os
import re
import shutil
# 1、生成评论
def createComment():
# 得到评论
arr = []
pathDir = os.listdir('./Comment/')
for index, value in enumerate(pathDir):
filepath2 = './Comment/' + value + '/内容.txt'
print(filepath2)
with open(filepath2, "r", encoding="utf-8") as f:
data = f.read()
arr.append(data)
# 写入评论
df = pd.read_excel('excel_comment.xlsx')
df['评论'] =['%s' % i for i in arr]
df.to_excel('test.xlsx',index=False)
print('ok')
# 2、生成文件夹
def createFolder():
arr = []
pathDir = os.listdir('./Comment/')
for index, value in enumerate(pathDir):
arr.append(value)
df = pd.read_excel('test.xlsx')
for index, row in df.iterrows():
str_index = str(index+1)
if (index+1) < 10:
str_index = '0' + str(index+1)
oldDirPath = './Comment/' + arr[index]
newDirPath = './Comment/'+str_index+' 旺旺:'+row['买家会员名']
os.rename(oldDirPath, newDirPath)
print('ok')
# 3、删除每个文件夹里面的 内容.txt
def deleteFile():
arr = []
pathDir = os.listdir('./Comment/')
for index, value in enumerate(pathDir):
filename = './Comment/' + value + '/内容.txt'
os.remove(filename)
print('ok')
# 4、去掉文件名前的数字
def renameFile():
arr = []
pathDir = os.listdir('./Comment/')
for index, value in enumerate(pathDir):
oldDirPath = './Comment/' + value
newDirPath = './Comment/'+value[3:]
os.rename(oldDirPath, newDirPath)
print('ok')
# 5、整理文件夹
def createNewFolder(dirname):
df = pd.read_excel('test.xlsx')
os.mkdir(dirname)
for index, row in df.iterrows():
username = row['买家会员名']
dir1='./Comment/旺旺:'+username
dir2='./'+dirname+'/旺旺:'+username
shutil.move(dir1, dir2)
shutil.rmtree('Comment')
shutil.move('./test.xlsx', './'+dirname+'/'+dirname+'.xlsx')
print('ok')
createComment()
createFolder()
deleteFile()
renameFile()
createNewFolder('2020-09-17 某某某 发膜-评语')# 传入参数