大数据技术(第5节实验课-----pandas处理表格信息和TCP通讯)

第一题 TCP通信程序,

要求: 用tcp连接 125.220.74.10:6001,并且发送你的学号,之后你会收到一个乘法表达式,
例如 12288*9933
把这个乘法表达式计算出来(要求用程序计算)
然后再发送回服务端,如果结果正确,你会在http://125.220.74.10:90/result.txt中
找到你的学号

import threading
import socket
#接收方法
def recvmsg(sock):
    while 1:
        try:
            data=sock.recv(1024)
            print(data.decode("UTF-8"))
            #保证发送不为空
            if(data==""):
                break
        except  ConnectionResetError as e:  #避免连接中断
            break
            
sock=socket.socket(socket.AF_INET,socket.SOCK_STREAM)  
sock.connect(("125.220.74.10",6001))     #连接服务器
t=threading.Thread(target=recvmsg,args=(sock,))  #创建一个接收线程
t.start()      
while 1:
    ss=input()  
    sock.send(ss.encode("UTF-8"))   #将字符串转化为utf-8字符

第二题 P131 第2题


filepath2="G:/大数据实验数据库/4.Python数据分析与应用/第4章/实训数据/Training_LogInfo.csv"
filepath3="G:/大数据实验数据库/4.Python数据分析与应用/第4章/实训数据/Training_Userupdate.csv"

#1.将用户信息转换为时间字符串
def fun1(LogInfo):
    print("进行转换前,Listinginfo1的类型为:",LogInfo["Listinginfo1"].dtypes)
    LogInfo["Listinginfo1"]=pd.to_datetime(LogInfo["Listinginfo1"])
    print("进行转换后,Listinginfo1的类型为:",LogInfo["Listinginfo1"].dtypes)
    print("\n")

#2.用year、month、week方法提取用户时间信息
def fun2(LogInfo):
    year=[i.year for i in LogInfo["Listinginfo1"]]
    month=[i.month for i in LogInfo["Listinginfo1"]]
    day=[i.day for i in LogInfo["Listinginfo1"]]
    week=[i.week for i in LogInfo["Listinginfo1"]]
    weekday=[i.weekday() for i in LogInfo["Listinginfo1"]]
    weekname=[i.weekday_name for i in LogInfo["Listinginfo1"]]

    print("登录数据表,前5条数据的年信息为",year[:5])
    print("登录数据表,前5条数据的月信息为",month[:5])
    print("登录数据表,前5条数据的日信息为",day[:5])
    print("登录数据表,前5条数据的周信息为",week[:5])
    print("登录数据表,前5条数据的星期信息为",weekday[:5])
    print("登录数据表,前5条数据的星期名信息为",weekname[:5])
    print("\n")

LogInfo=pd.read_csv(filepath2,sep=",",encoding="GBK")
fun1(LogInfo)
fun2(LogInfo)
Userupdate=pd.read_csv(filepath3,sep=",",encoding="GBK")
fun1(Userupdate)
fun2(Userupdate)


#计算时间差
timeDelta=Userupdate["Listinginfo1"]-LogInfo["Listinginfo1"]

print("以天为单位",timeDelta[:5].values/np.timedelta64(1, 'D'))
print("以小时为单位",timeDelta[:5].values/np.timedelta64(1, 'h'))
print("以分钟为单位",timeDelta[:5].values/np.timedelta64(1, 'm'))

第三题 P131 第3题


import pandas as pd
import numpy as np

filepath2="G:/大数据实验数据库/4.Python数据分析与应用/第4章/实训数据/Training_LogInfo.csv"
filepath3="G:/大数据实验数据库/4.Python数据分析与应用/第4章/实训数据/Training_Userupdate.csv"

LogInfo=pd.read_csv(filepath2,sep=",",encoding="GBK")
Userupdate=pd.read_csv(filepath3,sep=",",encoding="GBK")



def fun1(LogInfo):
    tx=LogInfo[:].groupby(by="Idx")#将数据以order_id进行分组
    print("分组后的最早时间")
    print(tx.agg({"Listinginfo1":np.min}))
    print("分组后的最最晚时间")
    print(tx.agg({"Listinginfo1":np.max}))
    print("时间的更改次数")
    print(tx.size())
    
    
print("登录表")  
fun1(LogInfo)
print("更新表")
fun1(Userupdate)

第四题

TCP通信程序, 加强难度
要求: 用tcp连接 125.220.74.10:6003,并且发送你的学号,之后你会收到一个乘法表达式,
例如 12288*9933
把这个乘法表达式计算出来(要求用程序计算)
然后再发送回服务端,所有过程要求在0.5秒内完成。
如果结果正确并且时间不超过0.5秒,你会在http://125.220.74.10:90/timu5.txt中
找到你的学号

import threading
import socket

result=""
flag=0
def recvmsg(sock):
    global result
    global flag
    flag1=0
    while 1:
        try:
            data=sock.recv(1024)
            print(data.decode("UTF-8"))
            if flag==flag1:
                result=data.decode("UTF-8")
                result=eval(result)
                result=str(result)
                sock.send(result.encode("UTF-8")) 
                flag1+=1
            
            #保证发送不为空
            if(data==""):
                break
        except  ConnectionResetError as e:  
            break
          
        
sock=socket.socket(socket.AF_INET,socket.SOCK_STREAM)  
sock.connect(("125.220.74.10",6003))    
try:
    t=threading.Thread(target=recvmsg,args=(sock,)) 
    t.start()  
    ss=input()  
    sock.send(ss.encode("UTF-8")) 
except:
    print("退出")
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值