分类: Python/Ruby
2022-10-11 17:54:01
#-*- coding : utf-8-*-
# coding:unicode_escape
'''
#! 本文件功能
#! 1、将rcontact.xlsx转化成new_rcontact.csv
#! 2、获取联系人的wxid
#! 另外两个文件都会使用此模块
'''
import os
import pandas as pd
filename = ''
if isExists := os.path.exists('./db_tables/rcontact.xlsx'):
isExists = os.path.exists('./db_tables/new_rcontact.csv')
if not isExists:
filename = './db_tables/rcontact.xlsx'
df = pd.read_excel(filename)
df.to_csv('./db_tables/new_rcontact.csv')
print("new_rcontact.csv 导出成功")
filename = './db_tables/new_rcontact.csv'
else:
print('rcontact.xlsx not exits')
print("请将数据库里的rcontact表导出\n命名为:rcontact.csv")
print("用Excel把文件导出为xlsx格式放到此目录下")
'''获取指定备注名的wxid'''
def get_one_wxid(conRemark):
if not filename:
print("联系人csv文件不存在")
else:
df = pd.read_csv(filename)
for row_index, row in df.iterrows():
if row['conRemark'] == conRemark :
wxid = row['username']
print(conRemark,wxid)
return wxid
'''获取自己的wxid'''
def get_self_wxid():
isExists = os.path.exists('./db_tables/userinfo.xlsx')
if not isExists:
print('userinfo.xlsx not exits')
print("请将数据库里的userinfo表导出\n命名为:userinfo.csv")
print("用Excel把文件导出为同名xlsx格式放到此目录下")
else:
isExists = os.path.exists('./db_tables/new_userinfo.csv')
if not isExists:
filename = './db_tables/userinfo.xlsx'
df = pd.read_excel(filename)
df.to_csv('./db_tables/new_userinfo.csv')
print("new_userinfo.csv 导出成功")
filename = './db_tables/new_userinfo.csv'
if isExists := os.path.exists(filename):
df = pd.read_csv(filename)
print(df)
for row_index, row in df.iterrows():
if 'wxid' in row['value']:
print("self_wxid =",row['value'])
return row['value']
else:
print("new_userinfo.csv文件不存在")
if __name__ == '__main__':
conRemark = '张三' #!备注名
filename = './db_tables/rcontact.csv'
get_one_wxid(conRemark)
get_self_wxid()
message.py
import pandas as pd
import os
import sys
import io
'''这个是自定义包'''
import rcontact
columns = ['msgId','msgSvrId','type','status','isSend','isShowTimer','createTime','talker','content','imgPath',
'reserved','lvbuffer','transContent','transBrandWording','talkerId',
'bizClientMsgId','bizChatId','bizChatUserId','msgSeq','flag','solitaireFoldInfo','historyId'
]
'''筛选一下文件头,其他好像没用'''
new_columns = ['createTime','isSend','talker','content','imgPath','reserved','talkerId','type','status']
Type = {
'1':'文字',
'3':'图片',
'43':'视频',
'-1879048185':'微信运动排行榜',
'5':'',
'47':'表情包',
'268445456':'撤回的消息',
'34':'语音',
'419430449':'转账',
'50':'语音电话',
'10000':'领取红包',
'10000':'消息已发出,但被对方拒收了。',
'822083633':'回复消息',
'922746929':'拍一拍',
'1090519089':'发送文件',
'318767153':'付款成功',
'436207665':'发红包',
}
sys.stdout = io.TextIOWrapper(sys.stdout.buffer,encoding='utf8')
'''判断路径是否存在,不存在则新建文件夹'''
def mkdir(path):
path = path.strip()
path = path.rstrip("\\")
if os.path.exists(path):
return False
os.makedirs(path)
return True
mkdir('.//db_tables')
'''
# ! 把原表处理一下过滤掉不必要的信息
# ! 作用不大可以忽略
'''
def read_all_data():
pdata = pd.read_csv('./db_tables/new_message.csv')
# del pdata['1']
print(pdata)
# pdata.to_csv('./db_tables/5.csv')
# msgId,msgSvrId,type,status,isSend
new = pd.DataFrame()
new.insert(0,'createTime',pdata['createTime'])
new.insert(1,'isSend',pdata['isSend'])
new.insert(2,'talker',pdata['talker'])
new.insert(3,'content',pdata['content'])
new.insert(4,'imgPath',pdata['imgPath'])
new.insert(5,'reserved',pdata['reserved'])
new.insert(6,'talkerId',pdata['talkerId'])
new.insert(7,'type',pdata['type'])
new.insert(8,'status',pdata['status'])
new.to_csv('./db_tables/simple_new_message.csv',index=False)
print("数据筛选成功!!!")
'''
#! 根据备注导出某个人的全部聊天信息
'''
def read_one_data(conRemark):
isExist = os.path.exists('./db_tables/simple_new_message.csv')
if not isExist:
print("simple_new_message.csv不存在\n请先执行上一个函数")
return
pdata = pd.read_csv('./db_tables/simple_new_message.csv')
print(pdata)
print("漫长的等待!!!")
wxid = rcontact.get_one_wxid(conRemark)
new_data = pd.DataFrame(columns=new_columns)
'''筛选出目标聊天记录'''
for row_index,row in pdata.iterrows():
if(row['talker']==wxid):
new_data.loc[len(new_data.index)] = list(row)
# print(new_data)
'''导入到新文件中'''
new_data.to_csv(f'./db_tables/{conRemark}.csv', index=False)
print(f"导出{conRemark}的聊天数据成功!!!")
''' 把时间数据转化成字符串防止数据舍入'''
def data_to_string():
isExist =外汇跟单gendan5.com os.path.exists('./db_tables/message.xlsx')
if not isExist:
print("message.xlsx不存在,请在将数据库里的message表导出成csv文件")
print("命名为message.csv,然后用Excel打开文件另存为message.xlsx")
print("放入./db_tables文件夹中")
return False
else:
isExist = os.path.exists('./db_tables/new_message.csv')
if not isExist:
df = pd.read_excel('./db_tables/message.xlsx')
df['createTime'].to_string()
print(df)
df.to_csv('./db_tables/new_message.csv')
else:
print("new_message.csv已就绪即将进行数据处理")
return True
if __name__ == '__main__':
# time_d()
# read_all_data()
# data_to_string()
status = data_to_string()
if status:
'''把message转化成csv'''
read_all_data()
'''输入备注名导出联系人的聊天信息'''
conRemark = '张三'
read_one_data(conRemark)