import datetime
class Message:#每天的消息,用時間戳進行排序
timestamp:int
messageId:int #消息次序用於重新寫入,使得有序
def __init__(self, timestamp, messageId):
self.timestamp= timestamp
self.messageId = messageId
def Conversion(timestamp):#將毫秒時間戳換算成日期
d = datetime.datetime.fromtimestamp(timestamp/1000)
dateStr=d.strftime("%Y-%m-%d")
#print(timestamp)
return dateStr
def getZhangsan(): #獲取有效數據
address = r"D:\code_test\Text_processing\records\record_" # 文件存儲地址
ansAddress=r"D:\code_test\Text_processing\ans" #答案存儲地址
cnt = 0#有效信息條數
for id in range(0, 1000): # txt 文本id的位置,打開1000個文本
print(id)
with open(address + str(id) + '.txt', 'r', encoding="utf-8") as f:
list1 = f.readlines()
row = len(list1) # 文本行數
for i in range(0, row):
if (list1[i][10:12] == '張三'):
timestamp=list1[i][28:41] #毫秒時間戳
#print(timestamp)
timeStr=Conversion(int(timestamp))
#文件寫入
fzs = open(ansAddress+"\\"+timeStr+'.txt', 'a')#寫入對應天數文件
fzs.write(list1[i])
fzs.close()
cnt += 1
print(cnt)
def sortMessageByTime():
ansAddress = r"D:\code_test\Text_processing\ans" # 答案存儲地址
for date in range(8,16): #按次序打開每天[8,15]日數據
dateStr="2022-04-"
if date<10:
dateStr+='0'
dateStr+=str(date)
print(dateStr)
MList=[] #用於時間戳排序
f=open(ansAddress+"\\"+ dateStr + '.txt', 'r')
list1 = f.readlines()
row = len(list1) # 文本行數
for i in range(0, row):
timestamp=int(list1[i][28:41])
MList.append( Message(timestamp,i) )
MList.sort(key=lambda e:e.timestamp)
#按時間寫入
fzs = open(ansAddress + "_sort_by_timestamp\\" + dateStr + '.txt', 'a') # 按時間次序寫入數據
for i in range(0, row):
msId=MList[i].messageId
fzs.write(list1[msId])
fzs.close()
f.close()
getZhangsan()
sortMessageByTime()