DISABLE ADBLOCK

ADBlock is blocking some content on the site

ADBlock errore

Question

I'm trying to write a script so that the output will be a .csv of 5 columns: 2 of data, followed by an empty column, and then 2 more columns of data. Here is what I get and here is what I'd like. For completeness, here is all of my code.

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]

I think that what's happening is that writer(item) writes two columns, and the next iteration of z overwrites those columns. EDIT: I noticed that I have myfile.close() in the function. That would explain it, but I don't know how to fix it.

No correct solution

OTHER TIPS

If, and only if you are writing a very simple CSV such as this, then you don't really need the csv package. Just open the file and write away:

f = open("out.csv", "w")
...
for line in lines:
    //line = ["col1","col2","","col3",""]
    csv_line = str.join(",",line)
    f.writeline(csv_line)
f.close()

If your data is more complex (i.e. not purely numeric) you will run into problems however.

Open the file only once, then:

wr = csv.writer(myfile)
wr.writerow(headers)
for x in data:
    # prepare row data
    # ... (the modified body of writer() function goes here)
    # write it as csv
    wr.writerow(row) # row is a list with 5 items
myfile.close()

When you open a file in 'wb' mode its size is truncated to zero and the previous content is lost. So when you call writer() function multiple times it replaces all previous content, only rows from the last call are left in the file.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow