【问题标题】:Storing data in Redis then fetch that data [closed]在 Redis 中存储数据然后获取该数据 [关闭]
【发布时间】:2016-01-19 15:36:19
【问题描述】:

我有一个包含四列的 Excel 文件。我想获取这些数据并将其存储在 MySQL 中。稍后我想从这里获取数据并存储在 Redis 中,然后对其进行验证。我已经完成了从 Excel 到 Python 的数据导入。

【问题讨论】:

  • 你还没有问过问题。

标签: python mysql database excel redis


【解决方案1】:

您必须将 4 列 excel 数据重塑为 1 列数据。 Matlab/GNU Octave 的 redis 客户端正在执行此操作,例如:https://github.com/markuman/go-redis/wiki/Data-Structure#arrays 注意在这个例子中,Matlab/Octave 使用的是 Column-Major-Order。 Python 正在使用 Row-Major-Order:https://en.wikipedia.org/wiki/Row-major_order

因此,您必须将 4 列 X 行数据保存为 redis 中的行主顺序列表 (RPUSH)。

示例

鉴于此 Excel 表

使用这个 python3 代码

    #!/usr/bin/python3
    # -*- coding: utf-8 -*-
    """
    Created on Tue Oct 20 23:02:53 2015

    @author: markus
    """

    import pandas as pd
    import redis

    # redis connection
    r = redis.StrictRedis(host='localhost', port=6379, db=0)

    # open the first worksheed
    df  = pd.read_excel('/home/markus/excel.xlsx',0) 

    # read in as a list
    # [[1, 'two', 'python'], ['excel', 'redis', 'action']]
    a   = list(df.T.itertuples()) 
    print("this is a, your excel list")
    print(a)
    for list in a:
        for value in list:
            r.rpush('myexceldata', str(value))

    # read all back to python
    b = r.lrange('myexceldata', '0', '-1')

    print("A1 becomes 0, B1 becomes 3 ...")
    print(b[3].decode('UTF-8'))

将其序列化为redis中的列表保存

    127.0.0.1:6379> lrange myexceldata 0 -1
    1) "1"
    2) "two"
    3) "python"
    4) "excel"
    5) "redis"
    6) "action"

这只是在 redis 中保存电子表格的一种方法。它始终属于您的数据结构以及您将如何处理它。

【讨论】:

  • 那我如何将这个excel数据导入redi呢?
  • 导入 python 列表中的每个 excel 单元格。 a[0] = #excelsheet_data (A1), a[1] = #excelsheet_data (A2) ... a[x] = #excelsheet_data(D100)。然后遍历a 并使用RPUSH yourkeyname a[iterator]
  • 你能说的更清楚吗谢谢 :)
  • 我附上了一个简约的例子。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-02-11
  • 1970-01-01
  • 2013-02-25
  • 1970-01-01
相关资源
最近更新 更多