我有一个清单:
lista.append(rede)
当我打印它时,显示:
[{'valor': Decimal('9000.00'), 'mes': 'Julho', 'nome': 'ALFANDEGA 1'}, {'valor': Decimal('12000.00'), 'mes': 'Julho', 'nome': 'AMAZONAS SHOPPING 1'}, {'valor': Decimal('600.00'), 'mes': 'Agosto', 'nome': 'ARARUAMA 1'}, {'valor': Decimal('21600.00'), 'nome': 'Rede Teste Integra\xc3\xa7\xc3\xa3o'}, {'valor': Decimal('3000.00'), 'mes': 'Agosto', 'nome': 'Mercatto Teste 1'}, {'valor': Decimal('5000.00'), 'mes': 'Agosto', 'nome': 'Mercatto Teste 2'}, {'valor': Decimal('8000.00'), 'nome': 'Rede Teste Integra\xc3\xa7\xc3\xa3o 2'}]
我想导出到csv文件并下载它,你能帮帮我吗?
答案 0 :(得分:0)
您可以使用此代码将数据转换为csv:
def Decimal(value):
#quick and dirty deal with your Decimal thing in the json
return value
data = [{'valor': Decimal('9000.00'), 'mes': 'Julho', 'nome': 'ALFANDEGA 1'}, {'valor': Decimal('12000.00'), 'mes': 'Julho', 'nome': 'AMAZONAS SHOPPING 1'}, {'valor': Decimal('600.00'), 'mes': 'Agosto', 'nome': 'ARARUAMA 1'}, {'valor': Decimal('21600.00'), 'nome': 'Rede Teste Integra\xc3\xa7\xc3\xa3o'}, {'valor': Decimal('3000.00'), 'mes': 'Agosto', 'nome': 'Mercatto Teste 1'}, {'valor': Decimal('5000.00'), 'mes': 'Agosto', 'nome': 'Mercatto Teste 2'}, {'valor': Decimal('8000.00'), 'nome': 'Rede Teste Integra\xc3\xa7\xc3\xa3o 2'}]
mes = []
nome = []
valor = []
for i in data:
mes.append(i.get('mes',""))
nome.append(i.get('nome',""))
valor.append(i.get('valor',""))
import csv
f = open("file.csv", 'wt')
try:
writer = csv.writer(f)
writer.writerow( ('mes', 'nome', 'valor') )
for i in range(0,len(mes)):
writer.writerow((mes[i], nome[i], valor[i]))
finally:
f.close()