下記のプログラムを実行したところこのようなエラーが表示されます。
last_sf_modified = 2018/06/14 22:51:30
last_lo_modified = 2000/01/01 00:00:00
Traceback (most recent call last):
File "s2h_updater.py", line 17, in <module>
updated = s2h.sf_get_updated_companies(last_lo_modified)
File "/home/dbcon/s2h.py", line 46, in sf_get_updated_companies
r[e] = r[e].encode('utf-8')
AttributeError: 'float' object has no attribute 'encode'
「Total_VC_Investment__c」がfloat型のためencodeができない状況です。
様々な方法を試しましたがうまくいかないためお力を貸していただければ幸いです。
float型をstring型に変換する方針です。
試した方法はコメントにしてあります。
宜しくお願いします。
python
1#!/usr/bin/env python 2# -*- coding: utf-8 -*- 3 4import s2hdb 5from datetime import datetime as dt 6from operator import add 7# 8# Salesforce handle Functions 9# 10 11def sf_get_last_modified_date(): 12 sf = s2hdb.sf_get_descriptor() 13 q = "SELECT LastModifiedDate FROM Account " + \ 14 "ORDER BY LastModifiedDate DESC LIMIT 1" 15 r = sf.query_all(q) 16 last_modified = dt.strptime(r["records"][0]["LastModifiedDate"], 17 '%Y-%m-%dT%H:%M:%S.000+0000') 18 return last_modified 19 20def sf_get_updated_companies(last_modified): 21 sf = s2hdb.sf_get_descriptor() 22 q = "SELECT Name, Website, Description, Description_JP__c, " + \ 23 "Core_Technology__c, Core_Technology_JP__c, Development_Status__c, " + \ 24 "Development_Status_JP__c, Application__c, Application_JP__c, Tags__c, " + \ 25 "crunchbase_City__c, crunchbase_Region__c, crunchbase_Country__c, " + \ 26 "crunchbase_Founded_Date__c, crunchbase__Latest_Round_Funding_Type_Series__c, " + \ 27 "crunchbase_Profile_Image_URL__c, CreatedDate, Total_VC_Investment__c " + \ 28 "FROM Account WHERE LastModifiedDate > " + \ 29 last_modified.strftime('%Y-%m-%dT%H:%M:%S.000+0000') 30 results = sf.query_all(q) 31 #print (results) 32 coms = [] 33 for r in results["records"]: 34 com = [] 35 elements = ['Name', 'Website', 'Description', 'Description_JP__c', 36 'Core_Technology__c', 'Core_Technology_JP__c', 'Development_Status__c', 37 'Development_Status_JP__c', 'Application__c', 'Application_JP__c', 'Tags__c', 38 'crunchbase_City__c', 'crunchbase_Region__c', 'crunchbase_Country__c', 39 'crunchbase_Founded_Date__c', 'crunchbase__Latest_Round_Funding_Type_Series__c', 40 'crunchbase_Profile_Image_URL__c', 'CreatedDate', 'Total_VC_Investment__c'] 41 elements = [str(e) for e in elements] 42 for e in elements: 43 # Convert NoneType Object as "" 44 if (r[e] is None): 45 r[e] = str("") 46 r[e] = r[e].encode('utf-8') 47 r[e] = r[e].replace('\'', '\'\'') 48 49 #if (e == 'crunchbase__Total_Funding_USD__c'): 50 #if (r[e] is None): 51 #r[e] = str("") 52 #r[e] = str('crunchbase__Total_Funding_USD__c') 53 #com.append('\'{0}\''.format(r[e])) 54 #else: 55 com.append('\'{0}\''.format(r[e])) 56 # Duplication check. 57 # It seems salesforce returns duplicated records. 58 duplicated = False 59 for c in coms: 60 if coms[0] == '\'{0}\''.format(r["Name"]): 61 duplicated = True 62 if (duplicated is True): 63 continue 64 65 coms.append(com) 66 #print (coms) 67 return coms 68 69# 70# Local DB handle functions 71# 72 73def lo_get_last_modified_date(): 74 con = s2hdb.lo_get_descriptor() 75 cur = con.cursor() 76 77 q = "SELECT last_modified from company ORDER BY last_modified DESC LIMIT 1" 78 cur.execute(q) 79 if (cur.rowcount == 0): 80 # We want to move all from salesforce, so just set very early date 81 last_modified = dt.strptime("2000/1/1 00:00:00", '%Y/%m/%d %H:%M:%S') 82 else: 83 last_modified = cur.fetchone()[0] 84 cur.close() 85 86 return last_modified 87 88def lo_update_companies(data): 89 if (len(data) == 0): 90 return 91 con = s2hdb.lo_get_descriptor() 92 cur = con.cursor() 93 94 coms = [] 95 for t in data: 96 coms.append(t[0:19]) 97 #print (coms) 98 q = "INSERT INTO company (legal_name, website, `desc`, desc_jp, core_tech, core_tech_jp, development_status, development_status_jp, " + \ 99 "application, application_jp, cat_name, City, Region, Country, founded_date, funding_series, picture, CreatedDate, total_funding) VALUES " + \ 100 ", ".join(map(lambda x : "(" + ",".join(x) + ")", coms)) + " " + \ 101 "ON DUPLICATE KEY UPDATE legal_name = VALUES(legal_name)" 102 #print (q) 103 cur.execute(q) 104 105 q = "INSERT INTO company (headquarter) " + \ 106 "SELECT GROUP_CONCAT(City, Region, Country SEPARATOR ', ') FROM company GROUP BY legal_name " 107 cur.execute(q) 108 con.commit() 109 cur.close()
回答1件
あなたの回答
tips
プレビュー