当前位置: 移动技术网 > IT编程>脚本编程>Python > 物流公司物品邮寄资费可视化分析

物流公司物品邮寄资费可视化分析

2020年07月05日  | 移动技术网IT编程  | 我要评论

最近正值毕业季,又到了毕业生向家里邮寄行李的时候了,然而随着物流行业的飞速发展,有很多家物流公司可供选择,比如德邦、顺丰、邮政、申通、京东等。各个公司的收费都不尽相同,因此整理了从吉林到各省份的资费标准并进行可视化分析以便选取合适的物流公司邮寄行李。

物品邮寄资费标准Excel表(express.xlsx)内容如下:

1、将Excel表中的数据插入到数据库中以便于后续数据筛选

注:程序采用python3.7版本,采用了python3内置的sqlite3模块进行数据库操作

创建数据库-》建表-》插入数据 代码如下:

# -*- codeing = utf-8 -*-
# @Time : 2020/7/3 17:03
# @Author : loadding...
# @File : Excel2db.py
# @Software : PyCharm
import xlrd
import sqlite3


def open_excel():
    try:
        book = xlrd.open_workbook('express.xlsx')  # 读取excel文件
    except:
        print('open excel file failed!')
    try:
        sheet = book.sheet_by_name('Sheet1')

        return sheet
    except:
        print('locate worksheet in excel failed!')


def init_db(dbpath):
    sql = '''
        create table express_info
        (
        id integer primary key autoincrement,
        name text,
        src_province text,
        dst_province text,
        first_weight numeric,
        first_price numeric,
        other_price numeric,
        premium_info text
        )
    '''
    # 创建表
    conn = sqlite3.connect(dbpath)
    cursor = conn.cursor()
    cursor.execute(sql)
    conn.commit()
    conn.close()


def main():
    init_db('express.db')  # 创建数据库并建表

    src_province = '长春'

    sheet = open_excel()

    for i in range(3, sheet.nrows - 2):
        # 德邦快递相关信息
        info = []  # 列表用于存储每条记录
        name = '德邦'
        dst_province = sheet.cell(i, 0).value  # 第i行第0列的单元格]
        first_weight = '3'
        first_price = sheet.cell(i, 1).value  # 首重

        other_price = sheet.cell(i, 2).value  # 续重
        premium_info = sheet.cell(sheet.nrows - 2, 0).value  # 保价费的信息
        info.append('"' + name + '"')
        info.append('"' + src_province + '"')
        info.append('"' + dst_province + '"')
        info.append(str(first_weight))
        info.append(str(first_price))
        info.append(str(other_price))
        info.append('"' + premium_info + '"')

        sql1 = '''
            insert into express_info(name,src_province,dst_province,first_weight,first_price,other_price,premium_info)
            values(%s)''' % ','.join(info)

        # 顺丰快递相关信息
        info = []  # 列表用于存储每条记录
        name = '顺丰'
        dst_province = sheet.cell(i, 4).value  # 第i行第0列的单元格
        first_weight = 20
        first_price = sheet.cell(i, 5).value  # 首重
        other_price = sheet.cell(i, 6).value  # 续重
        premium_info = sheet.cell(sheet.nrows - 2, 4).value  # 保价费的信息
        info.append('"' + name + '"')
        info.append('"' + src_province + '"')
        info.append('"' + dst_province + '"')
        info.append(str(first_weight))
        info.append(str(first_price))
        info.append(str(other_price))
        info.append('"' + premium_info + '"')

        sql2 = '''
                    insert into express_info(name,src_province,dst_province,first_weight,first_price,other_price,premium_info)
                    values(%s)''' % ','.join(info)

        # 邮政快递相关信息
        info = []  # 列表用于存储每条记录
        name = '邮政'
        dst_province = sheet.cell(i, 8).value  # 第i行第0列的单元格]
        first_weight = 1
        first_price = sheet.cell(i, 9).value  # 首重
        other_price = sheet.cell(i, 10).value  # 续重
        premium_info = sheet.cell(sheet.nrows - 2, 8).value  # 保价费的信息
        info.append('"' + name + '"')
        info.append('"' + src_province + '"')
        info.append('"' + dst_province + '"')
        info.append(str(first_weight))
        info.append(str(first_price))
        info.append(str(other_price))
        info.append('"' + premium_info + '"')

        sql3 = '''
                           insert into express_info(name,src_province,dst_province,first_weight,first_price,other_price,premium_info)
                           values(%s)''' % ','.join(info)

        # 申通快递相关信息
        info = []  # 列表用于存储每条记录
        name = '申通'
        dst_province = sheet.cell(i, 12).value  # 第i行第0列的单元格]
        first_weight = 1
        first_price = sheet.cell(i, 13).value  # 首重
        other_price = sheet.cell(i, 14).value  # 续重
        premium_info = sheet.cell(sheet.nrows - 2, 12).value  # 保价费的信息
        info.append('"' + name + '"')
        info.append('"' + src_province + '"')
        info.append('"' + dst_province + '"')
        info.append(str(first_weight))
        info.append(str(first_price))
        info.append(str(other_price))
        info.append('"' + premium_info + '"')

        sql4 = '''
                           insert into express_info(name,src_province,dst_province,first_weight,first_price,other_price,premium_info)
                           values(%s)''' % ','.join(info)

        # 京东快递相关信息
        info = []  # 列表用于存储每条记录
        name = '京东'
        dst_province = sheet.cell(i, 16).value  # 第i行第0列的单元格]
        first_weight = 20
        first_price = sheet.cell(i, 17).value  # 首重
        other_price = sheet.cell(i, 18).value  # 续重
        premium_info = sheet.cell(sheet.nrows - 2, 16).value  # 保价费的信息
        info.append('"' + name + '"')
        info.append('"' + src_province + '"')
        info.append('"' + dst_province + '"')
        info.append(str(first_weight))
        info.append(str(first_price))
        info.append(str(other_price))
        info.append('"' + premium_info + '"')

        sql5 = '''
                           insert into express_info(name,src_province,dst_province,first_weight,first_price,other_price,premium_info)
                           values(%s)''' % ','.join(info)

        conn = sqlite3.connect('express.db')
        cursor = conn.cursor()
        cursor.execute(sql1)
        cursor.execute(sql2)
        cursor.execute(sql3)
        cursor.execute(sql4)
        cursor.execute(sql5)

        cursor.close()
        conn.commit()
        conn.close()


if __name__ == '__main__':
    main()

完成后的数据库表内容如下:

2、数据可视化,原寄地为吉林,输入要查询的目的地,就打印出各物流公司的可视化对比结果

python代码如下:

#-*- codeing = utf-8 -*-
#@Time : 2020/7/2 21:44
#@Author : loadding...
#@File : express_price.py
#@Software : PyCharm

#分析从吉林至各省份邮寄快递采用哪家快递最划算
import numpy as np
import math
import matplotlib.pyplot as plt
import sqlite3

plt.rcParams['font.sans-serif']=['SimHei'] #用来正常显示中文标签
plt.rcParams['axes.unicode_minus']=False #用来正常显示负号
def main(province):
    sql='''
    select name,first_weight,first_price,other_price from express_info where dst_province='%s'
    '''%province
    conn = sqlite3.connect('express.db')
    cursor = conn.cursor()
    cursor=cursor.execute(sql)
    if len(list(cursor)) == 0:#这一个语句会造成cursor发生变化吗?
        print('抱歉!该省份不在统计范围内!')
        exit()
    cursor = cursor.execute(sql)#为什么还需要重新执行sql
    for row in cursor:
        if row[0]=='德邦':
            db_first_weight=row[1]
            db_first_price=row[2]
            db_other_price=row[3]
        elif row[0]=='顺丰':
            sf_first_weight=row[1]
            sf_first_price=row[2]
            sf_other_price=row[3]
        elif row[0]=='邮政':
            yz_first_weight=row[1]
            yz_first_price=row[2]
            yz_other_price=row[3]
        elif row[0]=='申通':
            st_first_weight=row[1]
            st_first_price=row[2]
            st_other_price=row[3]
        elif row[0]=='京东':
            jd_first_weight=row[1]
            jd_first_price=row[2]
            jd_other_price=row[3]
    cursor.close()
    conn.commit()
    conn.close()
    x = np.arange(0, 30, 1)
    y_1=[]
    y_2=[]
    y_3=[]
    y_4=[]
    y_5=[]
    y_max=[]#用于规划y轴长度
    # 德邦快递
    for t1 in x:
        if t1 <= db_first_weight:
            y1 = db_first_price
        else:
            y1 = db_first_price + db_other_price * (t1 - db_first_weight)
            y_max.append(y1)
        y_1.append(y1)
    # 顺丰快递&京东快递
    for t2 in x:
        if t2 <= sf_first_weight:
            y2 = sf_first_price
            y5=jd_first_price
        else:
            y2 = sf_first_price + sf_other_price * (t2 - sf_first_weight)
            y5 = jd_first_price + jd_other_price * (t2 - jd_first_weight)
            y_max.append(y2)
            y_max.append(y5)
        y_2.append(y2)
        y_5.append(y5)
    # 邮政快递&申通快递
    for t3 in x:
        if t3 <= yz_first_weight:
            y3 = yz_first_price
            y4 = st_first_price
        else:
            y3 = yz_first_price + yz_other_price * (t3 - yz_first_weight)
            y4 = st_first_price + st_other_price * (t3 - st_first_weight)
            y_max.append(y3)
            y_max.append(y4)
        y_3.append(y3)
        y_4.append(y4)
    plt.plot(x, y_1, label='德邦')
    plt.plot(x, y_2, label='顺丰')
    plt.plot(x, y_3, label='邮政')
    plt.plot(x, y_4, label='申通')
    plt.plot(x, y_5, label='京东')
    plt.xlabel('重量/kg')
    plt.ylabel('总价/元')
    plt.xticks(np.arange(0,31,1))
    # plt.yticks(np.arange(0,(st_first_price + st_other_price * (31 - st_first_weight)),5))
    plt.yticks(np.arange(0,max(y_max),5))
    plt.title(province+'(省/市)快递资费明细图')
    plt.legend()
    plt.show()

if __name__ == '__main__':
    province=input('请输入目的省份:')
    main(province)

列举几个省份的结果如下:

河南省:

湖北省:

北京市:

根据可视化输出结果可以帮助我们选择价格较便宜的物流公司

 

本文地址:https://blog.csdn.net/qq_41137110/article/details/107118673

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网