Hopefully I’m not missing something obvious…
I’m creating a function that will ease in creating specifically formatted JSON data from a Django query. However, this should be a purely python question.
I’d like to be able to convert all values with a given index to a specified format with a function like the folowing:
data = [['foo1','bar1'],['foo2','bar2']]
format = {1:'VALUE says Hello world'}
>> some_function(data, format)
[['foo1','bar1 says Hello World'],['foo2','bar2 says Hello World']]
Basic idea:
def some_function(data, format):
for row in data:
for count, value in enumerate(row):
if format.has_key(count):
#do something to replace 'VALUE' with value
else:
#just use value, no changes
return formatted_data
The root of this question is that the data goes directly to web and I’d like to define specific formatting based on what I’m passing through the generic function.
Edit for clarity:
What should I add to some_function() to convert the appropriate values in the data list to the format defined by the format dictionary?
The format you can use is explained in the
docs.