1

What's wrong with this code? It downloads the CSV fine, but puts all of the data into one column. If i don't have the ([]) brackets in there, it breaks each character into a seperate cell. Any ideas how to fix this?

import re, urllib, urllib2

class Spreadsheet(object):
    def __init__(self, key):
        super(Spreadsheet, self).__init__()
        self.key = key

class Client(object):
    def __init__(self, email, password):
        super(Client, self).__init__()
        self.email = email
        self.password = password

    def _get_auth_token(self, email, password, source, service):
        url = "https://www.google.com/accounts/ClientLogin"
        params = {
            "Email": email, "Passwd": password,
            "service": service,
            "accountType": "HOSTED_OR_GOOGLE",
            "source": source
        }
        req = urllib2.Request(url, urllib.urlencode(params))
        return re.findall(r"Auth=(.*)", urllib2.urlopen(req).read())[0]

    def get_auth_token(self):
        source = type(self).__name__
        return self._get_auth_token(self.email, self.password, source, service="wise")

    def download(self, spreadsheet, gid=0, format="csv"):
        url_format = "https://spreadsheets.google.com/feeds/download/spreadsheets/Export?key=%s&exportFormat=%s&gid=%i"
        headers = {
            "Authorization": "GoogleLogin auth=" + self.get_auth_token(),
            "GData-Version": "3.0"
        }
        req = urllib2.Request(url_format % (spreadsheet.key, format, gid), headers=headers)
        return urllib2.urlopen(req)

if __name__ == "__main__":
    import getpass
    import csv

    email = "username@gmail.com" # (your email here)
    password = getpass.getpass()
    spreadsheet_id = "18ZzsXwLtwKmm6-nUDXjG4Zknszt8SEQHPmzii_Sz9zY" # (spreadsheet id here)

    # Create client and spreadsheet objects
    gs = Client(email, password)
    ss = Spreadsheet(spreadsheet_id)

    # Request a file-like object containing the spreadsheet's contents
    csv_file = gs.download(ss)



    writer = csv.writer(open(r"C:\Users\me\Desktop\csv.csv", 'wb'))
        for row in csv_file:
            writer.writerow([row])

Here is my output: enter image description here

ivan_pozdeev
  • 33,874
  • 19
  • 107
  • 152
MjonesGEO
  • 113
  • 5
  • possible duplicate of [Exporting a Unicode .csv (comma separated) file to either Excel 2003 Or Excel 2007 results in all columns ending up in the first column in Excel](http://stackoverflow.com/questions/290037/exporting-a-unicode-csv-comma-separated-file-to-either-excel-2003-or-excel-20) – ivan_pozdeev Feb 10 '15 at 22:51
  • I'm using Excel 2010 – MjonesGEO Feb 11 '15 at 17:31

1 Answers1

0

csv_file is currently just a list that's 1 item long, thus the loop for row only goes once.

Read in each row of csv_file first, then write it elsewhere.

import csv

with open(csv_file, 'rb') as f:
reader = csv.reader(f)
for row in reader:
    writer.writerow([row])

https://docs.python.org/2/library/csv.html has more

Jonathan Epstein
  • 369
  • 2
  • 12