Tell me more ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I have a script that can take the final redirection url and save it into CSV file.

The script write codes in 1 column for example A1 then A3 then A5

How to make it write the codes by rows for example A1 B1 C1 D1

please see this the red color that what i want, the blue color that is the final result and i don't want it to be like that ( the list in 1 column and goes down A1 A3 A5 and there are a spaces between every cell !! )

this is my final script

import urllib2
import csv
import sys

url = 'http://www.test.com'

u = urllib2.urlopen(url)
localfile = open('C:\\test\\file.csv', 'a')
writer = csv.writer(localfile)
writer.writerow([u.geturl()])
localfile.close()
share|improve this question
it could be the csv you're getting. when something happens that you don't understand, it's best to see what's going on. use csv.reader and then print out some of it so that you can see it and maybe then you will understand what is wrong. If not, post what you see and maybe we can help! – Ryan Saxe 6 hours ago
hi ryan but i want to write not to read the list ( i want to write a list of urls by columns instead of rows – user2564147 5 hours ago

2 Answers

up vote 0 down vote accepted

Why not just create CSV by yourself if it will have only one row?

import urllib2

url = 'http://www.google.com'

u = urllib2.urlopen(url)
localFile = open('C:\\file.csv', 'ab')
localFile.write(u.geturl() + ",")

localFile.close()
share|improve this answer
you are amazing!!!! now it works !!! i cannot do it because the www.test.com is a private urls and urls will redirect me to a new urls so i have to save all the urls side by side then i have to import the csv file ( with a huge amount of urls ) so i cannot do it by myself – user2564147 3 hours ago
and thank you so much ton1c – user2564147 3 hours ago

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']
>>> 
share|improve this answer
hi the script is for save the redirected url and save it ( every time it gives me a new url ) so i cannot write a specific list because it's unlimited list. and what i want is write the url every time when i run the script to a new column such as A1 B1 C1 rather than A1 A3 A5 what you suggest to change ? – user2564147 5 hours ago
I think the list is still a better choice. For example, I have a global list L, every time I get a url I do L.append(url). See the list is not a specific one, it changes dynamicaly. After you get the number of urls you want in a row, such as 10. You call writerow. In csv, a list is a record, each value in the record is in the column. So if you do want write a row one time, I think you need to first read the row, and then append the new url to it. But this nothing different to directly using a list. @user2564147 – zhangyangyu 5 hours ago
Now I understand what you mean is just like a callback right? It can not be executed all the time so every time there is only one url. In this case, just as I said above, you may need to read the record first and then append the new url to the record and write it back. I can not come up with any better idea. Sorry :(@user2564147 – zhangyangyu 4 hours ago
thank you zhangyangyu but i'm beginner and i didn't understand you! could you please write the code to let me see it ? and the reason why i want to write it by column because i want to import the file when i finish ( so it has to be CSV Comma delimited ) so it has to listed by columns instead of rows or i cannot import it – user2564147 4 hours ago
I have updated my answer, you may have a look.@user2564147 – zhangyangyu 4 hours ago
show 4 more comments

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.