I have the following Python code which takes data from cassandra database. What i want to achieve is to have json encoded variable at the end. However, not surprisingly, my code only attaches the latest data row to the variable.

How can I combine or append all of the data to a single variable?

for key, columns in crime.get_range():
    data = json.dumps(columns)

Thanks

I am using Pycassa as a library to access Cassandra

有帮助吗?

解决方案

Collect all the data in a list first, then encode that list:

data = json.dumps([columns for key, columns in crime.get_range()])
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top