1

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()
2
  • 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! Commented Jul 10, 2013 at 7:15
  • 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 Commented Jul 10, 2013 at 8:56

2 Answers 2

1

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()
Sign up to request clarification or add additional context in comments.

1 Comment

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
0

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']
>>> 

6 Comments

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 ?
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
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
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
thank you zhangyangyu and i think you didn't understand me the result is perfect ( the same result that i want ) but the script print the result incorrectly first this is the code i.imgur.com/CmA6Fr4.jpg so it's good until now but the csv file is like this i.imgur.com/J6HSG0X.jpg but i don't want it to be like this because i CANNOT import the csv file as CSV comma delimited because it's printed by rows ( A1 A3 A5 then you cannot import it) so i want it to be like this i.imgur.com/BQk2eQm.jpg if it's like this photo so i can import it i hope you understand me and thank you
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.