Nunjucks Json模仿客户端

时间:2017-03-14 02:19:53

标签: json nunjucks

import urllib2

listOfStocks = ["AAPL", "MSFT", "GOOG", "FB", "AMZN"]

urls = []

for company in listOfStocks:
    urls.append('http://real-chart.finance.yahoo.com/table.csv?s=' + company + '&d=6&e=28&f=2015&g=m&a=11&b=12&c=1980&ignore=.csv')

# Create a CSV on your desktop to hold that data that id scraped from the web . . .
Output_File = open('C:/Users/Excel/Desktop/Historical_Prices.csv','w')

New_Format_Data = ''

for counter in range(0, len(urls)):

    Original_Data = urllib2.urlopen(urls[counter]).read()

    if counter == 0:
        New_Format_Data = "Company," + urllib2.urlopen(urls[counter]).readline()

    rows = Original_Data.splitlines(1)

    for row in range(1, len(rows)):

        New_Format_Data = New_Format_Data + listOfStocks[counter] + ',' + rows[row]

Output_File.write(New_Format_Data)
Output_File.close()

[{" value":" New"," id":" ab"},{" value&#34 ;:"打开"," id":" cd"},{"值":"关闭",& #34; id":" ef"}]         我只是想用nunjucks模板将json对象转换为下面的格式。新的ab  打开cd  关闭ef 你能帮我实现这个吗?

1 个答案:

答案 0 :(得分:0)

<div>
    {% for item in data %}
      {{item.value}} {{item.id}}
    {% endfor %}
</div>

其中data是您的对象数组。