python csv list by rows instead of columns


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 <a href="http://i.imgur.com/Gl5jdrf.jpg" rel="nofollow">this</a> 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()


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()


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.

<strong>edit:</strong> <br /> 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'] >>>


  • Finding specific URLs from a list of URLs using Python
  • Count the number of pixels by color from an image loaded into a numpy array
  • Get access token from Paypal in Python - Using urllib2 or requests library
  • Creating executable with Py2exe and matplotlib errors
  • Save or export weights and biases in TensorFlow for non-Python replication
  • How to check if the url redirect to another url using Python
  • imgurpython.helpers.error.ImgurClientRateLimitError: Rate-limit exceeded
  • The bug of mongoid returning first document when invoking last?
  • Which is the best way to get the tags from Youtube with python?
  • POST request via urllib/urllib2?
  • Why is 0x20 subtracted from the stack pointer in the prologue of this function's code?
  • Java String.format adding spaces to integers
  • How to find F(x,0) when F(x,i) = F(x-1,i) xor F(x-1, i+1) xor … F(x-1,n) in less than linear time
  • Multiplying arrays with scalars and adding in VBA
  • AttributeError: 'Response' object has no attribute 'body_as_unicode' scrapy for
  • Quickly or concisely determine the longest string per column in a row-based data collection
  • Can I use worksheet_change for a specific column only?
  • After getting Cell Info, EXC_BAD_ACCESS is thrown
  • Updating and removing unique join relationships in CakePHP
  • Python to parent/child JSON
  • Julia: How to give multiple workers access to functions that are 'include(…)' into a modul
  • Validate jQuery plugin, field not required
  • Center align outputs in ipython notebook
  • Reading a file into a multidimensional array
  • custom UITableViewCell with image for highlighting
  • preg_replace Double Spaces to tab (\\t) at the beginning of a line
  • does jqgrid support a multiple checkbox list for editing
  • Display issues when we change from one jquery mobile page to another in firefox
  • Deselecting radio buttons while keeping the View Model in synch
  • Optimizing database types to compact database (SQLite)
  • Cross-Platform Protobuf Serialization
  • Sending data from AppleScript to FileMaker records
  • JSON with duplicate key names losing information when parsed
  • vba code to select only visible cells in specific column except heading
  • Do I've to free mysql result after storing it?
  • Jquery - Jquery Wysiwyg return html as a string
  • bootstrap to use multiple ng-app
  • How to get icons for entities from eclipse?
  • Turn off referential integrity in Derby? is it possible?
  • JaxB to read class hierarchy