存储数据(表)的最佳方式并易于访问以表示它
Posted
技术标签:
【中文标题】存储数据(表)的最佳方式并易于访问以表示它【英文标题】:Best way to store data(table) and easy acces to represent it 【发布时间】:2015-01-09 12:11:18 【问题描述】:我正在使用 django 框架,我找到了一种方法来存储我通过 RPC 调用收到的数据。此数据遵循 JSON 格式:
"header":["data_enviament","nom_auditor","matricula","bastidor"],"data":["data_enviament":"05/1/2014","nom_auditor":"Brutus Brutus, Marc","matricula":"1234FRX","bastidor":"192891478kjhda","data_enviament":"05/2/2014","nom_auditor":"Pepito Rudolf, Margarita","matricula":"2234FRX","bastidor":"192891478kjhda","data_enviament":"05/5/2014","nom_auditor":"Patrick Linda, Judith","matricula":"5234FRX","bastidor":"192891478kjhda"],"count":2
我将这些数据存储到一个矩阵中(在控制器点),代码是:
for i in range(len(tabla['header'])):
array[0][i] = tabla['header'][i]
x = 1
for data in tabla['data']:
for i in range(len(tabla['header'])):
array[x][i] = data[array[0][i]]
x = x + 1
然后我通过渲染函数将这些数据解析到模板中并表示成一个html表格。 我做得很好,或者可能有其他方法可以做得更好?
【问题讨论】:
【参考方案1】:您可以将数据转换为列表的列表,并根据标题保持项目的顺序。
从 shell 演示:
>>> from django.template import Template, Context
>>> data = "header":["data_enviament","nom_auditor","matricula","bastidor"],"data":["data_enviament":"05/1/2014","nom_auditor":"Brutus Brutus, Marc","matricula":"1234FRX","bastidor":"192891478kjhda","data_enviament":"05/2/2014","nom_auditor":"Pepito Rudolf, Margarita","matricula":"2234FRX","bastidor":"192891478kjhda","data_enviament":"05/5/2014","nom_auditor":"Patrick Linda, Judith","matricula":"5234FRX","bastidor":"192891478kjhda"],"count":2
>>>
>>> data = [[item[header] for header in data['header']] for item in data['data']]
>>> c = Context('data': data)
>>> template = """
<table>
% for row in data %
<tr>
% for item in row %
<td> item </td>
% endfor %
</tr>
% endfor %
</table>
"""
>>> t = Template(template)
>>> print t.render(c)
<table>
<tr>
<td>05/1/2014</td>
<td>Brutus Brutus, Marc</td>
<td>1234FRX</td>
<td>192891478kjhda</td>
</tr>
<tr>
<td>05/2/2014</td>
<td>Pepito Rudolf, Margarita</td>
<td>2234FRX</td>
<td>192891478kjhda</td>
</tr>
<tr>
<td>05/5/2014</td>
<td>Patrick Linda, Judith</td>
<td>5234FRX</td>
<td>192891478kjhda</td>
</tr>
</table>
【讨论】:
以上是关于存储数据(表)的最佳方式并易于访问以表示它的主要内容,如果未能解决你的问题,请参考以下文章