我正在尝试编写一个脚本,以便输出为 5 列的 .csv:2 列数据,然后是一个空列,然后是另外 2 列数据。这就是我得到的,这就是我想要的。为了完整起见,这是我的所有代码。
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()
def writer(item):
q = data.index(item)
headers = [names[q], 'Percent Change', None]
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)
myfile = open('prices.csv', 'wb')
wr = csv.writer(myfile)
wr.writerow(headers)
wr.writerows(f)
myfile.close()
z = [writer(x) for x in data]
我认为正在发生的事情是writer(item)
写入两列,并且下一次迭代会z
覆盖这些列。编辑:我注意到我有myfile.close()
这个功能。这可以解释它,但我不知道如何解决它。