TypeError:类型对象' set'不是JSON可序列化的

时间:2017-05-29 15:40:47

标签: python json pyodbc google-sheets-api

我编写了一个python脚本,使用PYODBC从生产数据库中提取数据,并根据该数据创建Google表格。所有工作都可以将数据写入新创建的Google表格。我收到了这个错误:

TypeError:类型对象' set'不是JSON可序列化的

我一开始认为这与我的查询结果中包含的日期有关,但删除它们并没有改变错误。

我是Python新手,甚至是Google Sheets API的新手。任何帮助将不胜感激。

这是我的Python代码:

from __future__ import print_function
import pyodbc
server = 'XXX.XXX.XXX.XXX'
database = 'TestDB'
username = 'XXXXXX'
password = 'XXXXXXXXXXXX'
driver= '{ODBC Driver 13 for SQL Server}'    
import time


from apiclient import discovery
from httplib2 import Http
from oauth2client import file, client, tools

SCOPES = 'https://www.googleapis.com/auth/spreadsheets'
store = file.Storage('storage.json')
creds = store.get()
if not creds or creds.invalid:
    flow = client.flow_from_clientsecrets('client_secret.json', SCOPES)
    creds = tools.run_flow(flow, store)
SHEETS = discovery.build('sheets', 'v4', http=creds.authorize(Http()))

data = {'properties': {'title': 'FirstTest [%s]' % time.ctime()}}
res = SHEETS.spreadsheets().create(body=data).execute()
SHEET_ID = res['spreadsheetId']
print('Created "%s"' % res['properties']['title'])

FIELDS = ('Service.ID', 'ServiceDate', 'Invoice_ID', 'IsPaid', 'OrderID', 'CustomerName', 'JobID', 'ParentJob_ID', 'ParentTask_ID', 'Worker_ID', 'FirstName', 'LastName', 'Tip')
cnxn = pyodbc.connect('DRIVER='+driver+';PORT=1433;SERVER='+server+';PORT=1443;DATABASE='+database+';UID='+username+';PWD='+ password)
cursor = cnxn.cursor()
cursor.execute("SELECT ScheduledService.ID, ScheduledService.ServiceDate, ScheduledService.Invoice_ID, ScheduledService.IsPaid, JobDetails.OrderID, JobDetails.Name, Job.ID AS JobID, Job.ParentJob_ID, Job.ParentTask_ID, JobAssignmentEntry.Worker_ID, [User].FirstName, [User].LastName, ScheduledService.Tip FROM ScheduledService INNER JOIN JobDetails ON ScheduledService.ID = JobDetails.OrderID INNER JOIN Job ON JobDetails.ID = Job.ID INNER JOIN JobAssignmentEntry ON Job.ParentJob_ID = JobAssignmentEntry.Job_ID INNER JOIN EmployeeDataNew ON JobAssignmentEntry.Worker_ID = EmployeeDataNew.ID INNER JOIN [User] ON EmployeeDataNew.ID = [User].ID WHERE (ScheduledService.ServiceDate BETWEEN DATEADD(DY,-49,GetDate()) AND DATEADD(DY,-1,GetDate())) AND (ScheduledService.Service_ID IN (23, 24))")
rows = cursor.fetchall()
cnxn.close()
rows.insert(0, FIELDS)
data = {'values'}

SHEETS.spreadsheets().values().update(spreadsheetId=SHEET_ID,
    range='A1', body=data, valueInputOption='RAW').execute()
print('Wrote data to Sheet:')
rows = SHEETS.spreadsheets().values().get(spreadsheetId=SHEET_ID,
    range='Sheet1').execute().get('values', [])
for row in rows:
    print(row)

以下是完整的追溯:

Traceback (most recent call last):
    File "D:\Users\Scott\Documents\Python\WeeklyAsystantRestaurantDeliveries_CSV.py", line 38, in <module>
        range='A1', body=data, valueInputOption='RAW').execute()
    File "C:\Program Files\Python36\lib\site-packages\googleapiclient\discovery.py", line 785, in method
        actual_path_params, actual_query_params, body_value)
    File "C:\Program Files\Python36\lib\site-packages\googleapiclient\model.py", line 151, in request
        body_value = self.serialize(body_value)
    File "C:\Program Files\Python36\lib\site-packages\googleapiclient\model.py", line 260, in serialize
        return json.dumps(body_value)
    File "C:\Program Files\Python36\lib\json\__init__.py", line 231, in dumps
        return _default_encoder.encode(obj)
    File "C:\Program Files\Python36\lib\json\encoder.py", line 199, in encode
        chunks = self.iterencode(o, _one_shot=True)
    File "C:\Program Files\Python36\lib\json\encoder.py", line 257, in iterencode
        return _iterencode(o, 0)
    File "C:\Program Files\Python36\lib\json\encoder.py", line 180, in default
        o.__class__.__name__)
TypeError: Object of type 'set' is not JSON serializable

0 个答案:

没有答案