使用python

时间:2015-06-04 14:51:20

标签: python csv import google-fusion-tables

http://fuzzytolerance.info/blog/2012/01/13/2012-01-14-updating-google-fusion-table-from-a-csv-file-using-python/我编辑了他的代码以导入必要的模块,但是我得到以下错误“AttributeError:'module'对象没有属性'urlencode'”。我运行代码,提示输入我的密码,我输入自己的谷歌帐户密码,然后代码给我错误信息,我需要在某处定义密码?

我想知道是否有人可以解雇我的代码或者告诉我如何避免此错误,甚至建议我将CSV导入到我自己的GOOGLE FUSION TABLE中的更简单方法

这是我的代码

import csv
from decimal import *
import getpass
from fusiontables.authorization.clientlogin import ClientLogin
from fusiontables import ftclient
nameAgeNick = 'C:\\Users\\User\\Desktop\\NameAgeNickname.txt'
# check to see if something is an integer
def isInt(s):
    try:
        int(s)
        return True
    except ValueError:
        return False

# check to see if something is a float
def isFloat(s):
    try:
        float(s)
        return True
    except ValueError:
        return False


# open the CSV file
ifile  = open(nameAgeNick, "rb")
reader = csv.reader(ifile)

# GFT table ID
tableID = "tableid"
# your username
username = "username"
# prompt for your password - you can hardcode it but this is more secure
password = getpass.getpass("Enter your password:")

# Get token and connect to GFT
token = ClientLogin().authorize(username, password)
ft_client = ftclient.ClientLoginFTClient(token)


# Loop through the CSV data and upload
# Assumptions for my data: if it's a float less than 0, it's a percentage
# Floats are being rounded to 1 significant digit
# Non-numbers are wrapped in a single quote for string-type in the updatate statement
# The first row is the column names and matches exactly the column names in Fustion tables
# The first column is the unique ID I'll use to select the record for updating in Fusion Tables
rownum = 0
setList = list()
nid = 0
for row in reader:
    # Save header row.
    if rownum == 0:
        header = row
    else:
        colnum = 0
        setList[:] = []
        for col in row:
            thedata = col
            # This bit rounds numbers and turns numbers < 1 into percentages
            if isFloat(thedata):
                if isInt(thedata) is False:
                    if float(thedata) < 1:
                        thedata = float(thedata) * 100
                    thedata = round(float(thedata), 1)
            else:
                thedata = "'" + thedata + "'"
            # make sql where clause for row
            setList.append(header[colnum] + "=" + str(thedata))
            nid = row[0]
            colnum += 1
        # get rowid and update the record
        rowid = ft_client.query("select ROWID from " + tableID + " where ID = " + nid).split("\n")[1]
        print( rowid)
        print( ft_client.query("update " + tableID + " set " + ",".join(map(str, setList)) + " where rowid = '" + rowid + "'"))
    rownum += 1

ifile.close()​

这是发生错误的模块:

#!/usr/bin/python
#
# Copyright (C) 2010 Google Inc.

""" ClientLogin.
"""

__author__ = 'kbrisbin@google.com (Kathryn Brisbin)'

import urllib, urllib2

class ClientLogin():
  def authorize(self, username, password):
    auth_uri = 'https://www.google.com/accounts/ClientLogin'
    authreq_data = urllib.urlencode({ //////HERE IS ERROR
        'Email': username,
        'Passwd': password,
        'service': 'fusiontables',
        'accountType': 'HOSTED_OR_GOOGLE'})
    auth_req = urllib2.Request(auth_uri, data=authreq_data)
    auth_resp = urllib2.urlopen(auth_req)
    auth_resp_body = auth_resp.read()

    auth_resp_dict = dict(
        x.split('=') for x in auth_resp_body.split('\n') if x)
    return auth_resp_dict['Auth']
​

0 个答案:

没有答案