结果集输出字符串拒绝使用 DynamoDB noSQL 强制转换为字典



我正在尝试将ResultSet转换为Dict,但是Python强制转换似乎不想正常工作。

与 AWS DynamoDB 的连接

c   = boto.dynamodb2.connect_to_region(aws_access_key_id="ACCESSKEYCODE222",aws_secret_access_key="SECRETKEYCODEKFJFJFJF",region_name="us-west-2")
table  = Table("reko_response",connection=c)
# Get the result set from DynamoDB
response = table.scan()
print(type(response)) # type of the response recieved = <class 'boto.dynamodb2.results.ResultSet'>

测试标准字符串到字典转换,该转换工作正常

string_of_dict = "{'name':'Frederik'}"
dict_of_string = ast.literal_eval(string_of_dict)
print(type(dict_of_string)) # Outputs Correction: <class 'dict'>

这将正常工作,字符串将强制转换为字典。

结果集强制转换为拒绝

强制转换为字典的字符串

for item in response:
string_item = json.dumps(str(dict(item)))
print(string_item)
print(type(string_item)) # Outputs correctly: <class 'str'>
# Cast to Dict Attempt 1
dict_item = json.loads(string_item)
print(type(dict_item)) # Expected Output: <class 'dict'> | Actual Output: <class 'str'> 
# Cast to Dict Attempt 2
dict_item = ast.literal_eval(string_item)
print(type(dict_item)) # Expected Output: <class 'dict'> | Actual Output: <class 'str'> 
sys.exit()

标准 Python 强制转换似乎都没有将结果集的字符串命名为字典。

这是怎么回事?

实际输出为

<class 'boto.dynamodb2.results.ResultSet'>
<class 'dict'>
"{'timestamp': 'Time: 2018-05-02 08:42:36.978210', 'ID': '39609c57-6ed5-4fc5-8baa-0007d1aeb7ef', 'Store': 'Coles:Brighton', 'Gender': 'Male', 'Age(high)': Decimal('43'), 'smile': False, 'smile_conf': Decimal('90.57567596435546875'), 'Emotion1': 'HAPPY', 'emo2-conf': Decimal('6.381499767303466796875'), 'Emotion3': 'CALM', 'emo1-conf': Decimal('19.021503448486328125'), 'Age(low)': Decimal('26'), 'emo3-conf': Decimal('5.70695400238037109375'), 'Emotion2': 'SAD'}"
<class 'str'>
<class 'str'>
<class 'str'>

预期输出和目标输出为

<class 'boto.dynamodb2.results.ResultSet'>
<class 'dict'>
"{'timestamp': 'Time: 2018-05-02 08:42:36.978210', 'ID': '39609c57-6ed5-4fc5-8baa-0007d1aeb7ef', 'Store': 'Coles:Brighton', 'Gender': 'Male', 'Age(high)': Decimal('43'), 'smile': False, 'smile_conf': Decimal('90.57567596435546875'), 'Emotion1': 'HAPPY', 'emo2-conf': Decimal('6.381499767303466796875'), 'Emotion3': 'CALM', 'emo1-conf': Decimal('19.021503448486328125'), 'Age(low)': Decimal('26'), 'emo3-conf': Decimal('5.70695400238037109375'), 'Emotion2': 'SAD'}"
<class 'str'>
<class 'dict'>
<class 'dict'>

意思是最后两个打印,是打印出应该被投射到字典但保留为字符串的字符串。

任何见解将不胜感激!

好的,事实证明我无法将结果集转换为字典,但是我可以通过引用我打算包含在新字典中的每个项目的值来将结果集的值分配给新字典。

例如,假设目标是按性别分隔项目,则解决方案的循环将是:

在循环中区分雄性和雌性:

for item in response:
female_smile = {}
male_smile = {}
print("start")
# Get Dicts of Females who are smiling over time
if item['Gender'] == 'Female':
female_smile['timestamp'] = item['timestamp']
female_smile['Store'] = item['Store']
female_smile['smile'] = item['smile']
female_smile_list.append(female_smile)
# Get Dicts of Males who are smiling over time  
else: 
male_smile['timestamp'] = item['timestamp']
male_smile['Store'] = item['Store']
male_smile['smile'] = item['smile']
male_smile_list.append(male_smile)

这个问题还没有完全回答,即"如何将结果集转换为字典"。也就是说,期望的结果是从结果集形成一个新的字典,这是通过将结果集的值分配给字典来实现的。

相关内容

  • 没有找到相关文章

最新更新