使用Python将列表导出并下载到csv文件

使用Python将列表导出并下载到csv文件,python,Python,我有一份清单: 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': 'ARARUAM

我有一份清单:

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文件并下载,您能帮我吗?

您可以使用此代码将数据转换为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()

看看Python CSV模块。而且,一次小小的努力也能让你得到帮助!Tks@grubjesic为您提供快速响应,但这些数据“数据”来自“lista.append(rede)”,作为“列表”中输入的数据,并转换为csv文件,以便我可以直接下载?我假设这些数据实际上是您的列表。e、 g.rede={'valor':Decimal('9000.00'),'mes':'Julho','nome':'ALFANDEGA 1'},并通过命令lista.append(rede)附加到名为lista的列表中。在我的示例中,我只是调用列表(lista)->数据。@leonardo santos你能用你的数据运行我的代码吗?