writer.writerow()
means write the list to a row. So, every time you call it there will be a new row. So the result is what you don't want, they are in one column. If you want to write them in one row. You'd better get a list, then put all the data you want in a row in it, such as l = [111, 222, 333, 444]
. Then call the writer.writerow(l)
just for one time. You can get what you want then.
edit:
If the script serve like a daemon, running all the time and waiting for the input:
#10 is the number you want in a row, you can assign it yourself.
L = []
urls = ['http://www.google.com', 'http://facebookcom', 'http://twitter.com']
for url in urls:
u = urllib2.urlopen(url)
L.append(u.geturl())
localfile = open('C:\\test\\file.csv', 'w')
writer = csv.writer(localfile)
writer.writerow(L)
localfile.close()
If the script serve like a callback, everytime it gets only one url. I'm quite sorry I don't see any API in csv
module to modify the file.
And as for me, I don't think in this case you need a csv file. One row in the csv usually represents a whole data structure, not like a list. If you want to import the file easily, you can just use the normal file, per url one line or splited by space. Next time when you need it you can simply use str
methods such as split
to treat it and turn it back into list quickly.
>>> 'http://www.google.com\nhttp://www.facebook.com\nhttp://www.twitter.com'.split('\n')
['http://www.google.com', 'http://www.facebook.com', 'http://www.twitter.com']
>>>