Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/332.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/redis/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python redis的动态命名元组_Python_Redis - Fatal编程技术网

Python redis的动态命名元组

Python redis的动态命名元组,python,redis,Python,Redis,我有一个csv文件,其中每行包含一个人的ID,然后是一组属性。我希望能够为每个人创建一个包含其所有属性的元组,然后将该元组命名为其ID的一些变体 所有这些元组将被添加到redis中的一个集合中进行存储 我似乎不知道如何创建以persons ID#命名的元组 我知道动态命名变量不是最好的做法,但我不希望将所有元组放在一个列表中,或者设置成一个redis集(这是必须的);它似乎效率低下,而且笨重 这是我现在拥有的代码: with open('personlist.csv','rb') as f: f

我有一个csv文件,其中每行包含一个人的ID,然后是一组属性。我希望能够为每个人创建一个包含其所有属性的元组,然后将该元组命名为其ID的一些变体

所有这些元组将被添加到redis中的一个集合中进行存储

我似乎不知道如何创建以persons ID#命名的元组

我知道动态命名变量不是最好的做法,但我不希望将所有元组放在一个列表中,或者设置成一个redis集(这是必须的);它似乎效率低下,而且笨重

这是我现在拥有的代码:

with open('personlist.csv','rb') as f:
for line in f:
        row = line.split(',')
        personID = row[0]
        attrb1 = row[1]
        attrb2 = row[2]
        attrb3 = row[3]
        # Need to name tuple here and define as (attrb1, attrb2, attrb3)
        r.lpush('allpersonslist',tuple)

这个例子需要额外的代码来运行。我假设您使用的是RedisAPI,比如RedisPy。变量
r
是与redis的开放连接

import pickle

with open('personlist.csv', 'rb') as f:
    for line in f:
        row = line.split(',')
        personID = row[0]
        attrb1 = row[1]
        attrb2 = row[2]
        attrb3 = row[3]
        #put the attributes in a tuple
        tuple = (attrb1, attrb2, attrb3)
        #serialize the tuple before adding it to the set
        r.set("person/%d" %personID,pickle.dumps(tuple,-1))

def getPerson(Id):
    return pickle.loads(r.get("person/%d" %Id))    

您可以调用
getPerson(5)
来返回与ID为5的人相关联的元组。

如果每个人都有max N属性,则存在基于哈希的独立于语言的解决方案。这里列出了3个为个人保存/读取/删除值的命令

  • HMSET'allpersonshash'personID:0 personID:1
  • HMGET'allpersonshash'personID:0 personID:1 personID:2。。。人物:N
  • HDEL'allpersonshash'personID:0 personID:1 personID:2。。。人物:N

  • 一种相当普遍的方法是将排序集与json blob一起使用,例如:

    ZADD userid, '{field1:value1,field2:value2}'
    

    你能详细谈谈你的问题吗?您只是想在redis中创建一个包含所有人的集合吗?为什么不使用
    csv
    模块?它将防止您在CSV文件中遇到各种语法问题。是的,使用redis py。理想情况下,我希望元组以personID命名,因此如果我有三个ID:101、102和103,我希望元组名称为101、102和103。并将其添加到redis集合中。这是为了我可以稍后返回并按ID号提取这些元组。我对这一切都有点陌生,所以我不太理解序列化。酸洗是否达到了我上面描述的效果?我根据您在此提供的其他信息更新了示例。您不需要在redis中使用集合或列表,只需设置/获取键即可。您必须使用pickle,因为您需要发送redis字符串。