我正在尝试编写一个看起来像这样的 csv 文件。我得到的是这个,所有数据都输出为两个长列。这是代码。
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]
有没有人对如何纠正这个有任何想法?谢谢。