Google Spreadsheet using Python to read and write to the contents of mangodb

Yesterday whim, now in the google docs spreadsheet is very strong, if my site data is not very big, so I'll save my data is not a very easy thing by spreadsheet you, in fact, my site just to save data for some products, but my product they can not be as many as eBay, then I try to put in a Google Spreadsheet data stored in it. 

With the goal, tool? To see Google Spreadsheets API it, ah, there it lay in Google Labs, but also good, it supports those language? Because I am the love of Python, but the API also supports Python, perfect !!! Well, to start to do it. To use this API, then the first step is to download a naturally called Google Python Client Library stuff. Using this premise is that you have Python client environment, and includes httplib and urllib these two modules, fortunately python2.6 have built these two modules, if you use the 2.6 version, this link to download directly from above , installed just fine. 

 python setup.py install

Enter python, we do some simple experiments.

>>> Import  gdata.spreadsheet.service AS Service >>>  Client  =  service.SpreadsheetsService () >>>  client.email  = " [email protected] " # your app or account gmail account >>>  client.password  = " ******** " # this is what the password, you certainly can not see >>>  client.source  = " IMAQ-Documents-v1 " # this is hard to explain, describe below about >>>  Client .ProgrammaticLogin ()  # use the above information to log in to Google  

    
    
   

Ah, well, I'm in the xD, Next, I want to know what my Documents in the spreadsheet, nonsense, of course I know that I can directly into my documents in what form I see, we have to inside the document comes with a demo experiment with stuff that is called the internal-Contact. First, if you want to access this document, we want to know the key of this document

>>>  docs  =  client.GetSpreadsheetsFeed()
>>>   for  i, entry  in  enumerate(docs.entry):
...     
print  entry.title.text , entry.id.text
... 
internal 
-  Contact http: // spreadsheets.google.com / feeds / spreadsheets / private / full / blablablabla__Y1ax999C
products http:
// spreadsheets.google.com / feeds / spreadsheets / private / full / blablablablabla_Z1fSm - nXQ
>>>

 see it? This document lists the two accounts I can access, entry.title.text is the name of the document, entry.id.text is this document feed address, the address of the api call you back when direct access to this document, which the / full / back of a string of blablabla this document is the key. With this key, we can access the document directly to the contents of the matter, first of all look at the table there are several sheet.

>>>  sheet  =  client.GetWorksheetsFeed(key)
>>>   for  i, entry  in  enumerate(sheet.entry):
...     
print  entry.title.text, entry.id.text
... 
Sheet1 http:
// spreadsheets.google.com / feeds / worksheets / blablablablabla__Y1ax999C / private / full / od1
>>>

 The results are given only a Sheet1, the back of the workbook is the key and this sheet of id, here is od1, now we can go and see the contents of this sheet

>>>  table  =  client.GetListFeed(key,wksht_id)
>>>   for  i, entry  in  enumerate(table.entry):
...     
for  column  in  entry.custom:
...             
print   " [%s:%s] " % (column, entry.custom[column].text)
... 
[comment:I
' m commenting on this contact form]
[timestamp: 2 / 15 / 2010   8 : 07 : 30 ]
[name:Sarah]
[email:[email protected]]
[comment:I want to join the group.]
[timestamp:
2 / 15 / 2010 13 is : 06 : 33 is ] [name: Larisa] [In Email: [email protected]] [Comment: IS  IT wotk] [timestamp: 2 / 15 / 2010 . 17 : 23 is : 55 ] [ name: Larisa] [in Email: [email protected]] [Comment: the Test] [timestamp: 2 / 16 / 2010 12 is : . 17 : 22 is ] [name: Jeremy] ...  # behind omitted  



 



 

The contents are out, a lot of A - -!

 

It seems very easy to access content or the next, we will be able to write the contents of cute mongodb to go, and how to use mongodb is not involved in the subject matter, since the content has been all out, you can use these write the contents of your favorite places to go, big table, mysql, oracle, or txt years. Use mongodb on the premise that you already have mongo environment and installed a python driver, called pymongo, then the following will then continue

>>>   from  pymongo  import  Connection
>>>  db  =  Connection( " localhost " ).test.contacts
>>>   for  i, entry  in  enumerate(table.entry):
...     obj 
=  {}
...     
for  col  in  entry.custom:
...             obj[col] 
=  entry.custom[col].text
...     db.save(obj)
... 
ObjectId(
' 4c2f9bad092ce508b2000000 ' )
ObjectId(
' 4c2f9bad092ce508b2000001 ' )
ObjectId(
' 4c2f9bad092ce508b2000002 ' )
ObjectId(
' 4c2f9bad092ce508b2000003 ' )
ObjectId(
' 4c2f9bad092ce508b2000004 ' )
ObjectId(
' 4c2f9bad092ce508b2000005 ' )
ObjectId(
' 4c2f9bad092ce508b2000006 ' )
ObjectId(
' 4c2f9bad092ce508b2000007 ' )
ObjectId(
' 4c2f9bad092ce508b2000008 ' )
ObjectId(
' 4c2f9bad092ce508b2000009 ' )
ObjectId(
' 4c2f9bad092ce508b200000a ' )
ObjectId(
' 4c2f9bad092ce508b200000b ' )
ObjectId(
' 4c2f9bad092ce508b200000c ' )
...

 Well, just that it contacts the contents of the written mongodb gone, you can go into the console mongo

use test
db.contacts.find()

 

 

Reproduced in: https: //www.cnblogs.com/Stephen/archive/2011/03/06/python-sync-gspreadsheet-mongodb.html

Guess you like

Origin blog.csdn.net/weixin_34293902/article/details/93754638