我正在尝试编写一个看起来像this的csv文件。我得到的是this,其中所有数据都输出为两个长列。这是代码。
import requests
import csv
item_dictionary = {'10350': 'Third-Age Full', '560':'Death Rune'}
item_ids = item_dictionary.keys()
url_template = 'http://www.grandexchangecentral.com/include/gecgraphjson.php?jsid=%r'
sites = []
for i in range(0, len(item_ids)):
result = url_template % item_ids[i]
sites.append(result)
def data_grabber(item):
url = item
r = requests.get(url, headers={'Referer': 'www.grandexchangecentral.com'})
data = r.json
prices = [i[1] for i in data]
return prices
data = map(data_grabber, sites)
names = item_dictionary.values()
filler = []
for i in range(0,365):
filler.append(None)
myfile = open('prices.csv', 'wb')
headers = [names[0], 'Percent Change', None, names[1], 'Percent Change', None]
wr = csv.writer(myfile)
wr.writerow(headers)
for x in data:
q = data.index(x)
a = data[q]
percents = [100.0 * a1 / a2 - 100 for a1, a2 in zip(a[1:], a)]
percents.insert(0, None)
f = zip(data[q], percents, filler)
wr.writerows(f)
myfile.close()
我认为问题出在f = zip(data[q], percents, filler)
上。它似乎写为data[0]
,然后再写为data[1]
下面的{{1}},当我想要它创建两个新列时。有没有人对如何纠正这个有任何想法?感谢。