存储数据库Redis-行政区划代码-5级12位-最终版

import json
import os
import time
import redis

joinStr = '\n'
fileSep = ','
fileDataEncoding = 'utf-8'


def timePlus():
    t = time.time()
    
    return '%s %.4f' % (time.strftime('%H:%M:%S', time.localtime(t)), t - int(t))


def getFileData(filePath):
    if os.path.exists(filePath):
        with open(filePath, 'r', encoding=fileDataEncoding) as r:
            fileData = r.readlines()
            
            # 这里改成生成器
            def newFileData():
                for _ in fileData:
                    if _:
                        _ = _.replace(joinStr, '').split(fileSep)
                        
                        _a = _[0]  # 名称
                        _b = _[1]  # 上级代码
                        _c = _[2]  # 当前代码
                        
                        yield json.dumps({
                            'code': _c,
                            'pcode': _b,
                            'name': _a
                        })
            
            return newFileData()


dataFilePath = r'行政区划代码-2020.txt'


def main():
    print('开始执行 当前时间: %s' % timePlus())
    
    if not os.path.exists(dataFilePath):
        raise Exception('数据文件不存在')
    
    print('数据读取开始 当前时间: %s' % timePlus())
    
    items = getFileData(dataFilePath)
    
    print('数据读取完成 当前时间: %s' % timePlus())
    
    print(items)
    
    r = redis.Redis(host='127.0.0.1', port=6379)
    
    print('数据存储开始 当前时间: %s' % timePlus())
    
    key = 'items'
    
    for k, _ in enumerate(items):
        r.rpush(key, _)
        print('\r%s 数据存储 %s 当前时间: %s' % (str(k).zfill(6), str(r.llen(key)).zfill(6), timePlus()), end='')
    
    print('数据存储结束 当前时间: %s' % timePlus())

    # 读取
    items = r.lrange(key, 0, -1)
    for k, _ in enumerate(d1):
        _ = json.loads(_.decode('unicode_escape'))
        
        print('\rcode: %s, pcode: %s, name: %s 当前时间: %s ' % (_.get('code'), _.get('pcode'), _.get('name'), timePlus()), end='')


if __name__ == '__main__':
    try:
        main()
        pass
    except (Exception, BaseException) as e:
        print(e)


在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
这部分环节算完了,三种文件保存,三种数据库保存;mysql和redis都是循环插入,感觉mysql插入慢很多,redis还没找到批量一次性插入的,mongodb和mysql都有的,后续可能再加个mysql的批量插入,看看速度如何

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

CY3761

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值