下記のプログラムを実行した際
「TypeError: a bytes-like object is required, not 'str'」というエラーが出てしまいます。
python2系から3系に移行したいと考えており現在3系で実行できるよう修正中です。
エラー解決のほかにも3系ならではのエラーが出そうな箇所がございましたらご指摘頂けると幸いです。
エラー内容
last_sf_modified = 2018/06/17 13:07:31
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 48, in sf_get_updated_companies
r[e] = r[e].replace(''', '''')
TypeError: a bytes-like object is required, not 'str'
pyhton
1#!/usr/bin/env python 2# -*- coding: utf-8 -*- 3 4import s2hdb 5from datetime import datetime as dt 6from operator import add 7import numpy as np 8import subprocess 9 10 11path = “/home/dbcon/0615auto_dl_h.py” 12# 13# Salesforce handle Functions 14# 15def sf_get_last_modified_date(): 16 sf = s2hdb.sf_get_descriptor() 17 q = "SELECT LastModifiedDate FROM Account " + \ 18 "ORDER BY LastModifiedDate DESC LIMIT 1" 19 r = sf.query_all(q) 20 last_modified = dt.strptime(r["records"][0]["LastModifiedDate"], 21 '%Y-%m-%dT%H:%M:%S.000+0000') 22 return last_modified 23 24def sf_get_updated_companies(last_modified): 25 sf = s2hdb.sf_get_descriptor() 26 q = "SELECT Name, Website, Description, Description_JP__c, " + \ 27 "Core_Technology__c, Core_Technology_JP__c, Development_Status__c, " + \ 28 "Development_Status_JP__c, Application__c, Application_JP__c, Tags__c, " + \ 29 "crunchbase_City__c, crunchbase_Region__c, crunchbase_Country__c, " + \ 30 "crunchbase_Founded_Date__c, crunchbase__Latest_Round_Funding_Type_Series__c, " + \ 31 "crunchbase_Profile_Image_URL__c, CreatedDate, Total_VC_Investment__c, Id " + \ 32 "FROM Account WHERE LastModifiedDate > " + \ 33 last_modified.strftime('%Y-%m-%dT%H:%M:%S.000+0000') 34 results = sf.query_all(q) 35 36 py = subprocess.call(“python %s” % path) 37 print(py) 38 #download() 39 #print (results) 40 coms = [] 41 for r in results["records"]: 42 com = [] 43 elements = ['Name', 'Website', 'Description', 'Description_JP__c', 44 'Core_Technology__c', 'Core_Technology_JP__c', 'Development_Status__c', 45 'Development_Status_JP__c', 'Application__c', 'Application_JP__c', 'Tags__c', 46 'crunchbase_City__c', 'crunchbase_Region__c', 'crunchbase_Country__c', 47 'crunchbase_Founded_Date__c', 'crunchbase__Latest_Round_Funding_Type_Series__c', 48 'crunchbase_Profile_Image_URL__c', 'CreatedDate', 'Total_VC_Investment__c', 'Id'] 49 elements = [str(e) for e in elements] 50 for e in elements: 51 52エラー部分----------------------------------------------------------- 53 # Convert NoneType Object as "" 54 if (r[e] is None): 55 r[e] = str("") 56 r[e] = str(r[e]).encode('utf-8') 57 r[e] = r[e].replace('\'', '\'\'') 58-----------------------------------------------------------ーーーーー 59 com.append('\'{0}\''.format(r[e])) 60 # Duplication check. 61 # It seems salesforce returns duplicated records. 62 duplicated = False 63 for c in coms: 64 if coms[0] == '\'{0}\''.format(r["Name"]): 65 duplicated = True 66 if (duplicated is True): 67 continue 68 #print(coms) 69 coms.append(com) 70 return coms 71 72 73# Local DB handle functions 74# 75 76def lo_get_last_modified_date(): 77 con = s2hdb.lo_get_descriptor() 78 cur = con.cursor() 79 80 q = "SELECT last_modified from company ORDER BY last_modified DESC LIMIT 1" 81 cur.execute(q) 82 if (cur.rowcount == 0): 83 # We want to move all from salesforce, so just set very early date 84 last_modified = dt.strptime("2000/1/1 00:00:00", '%Y/%m/%d %H:%M:%S') 85 else: 86 last_modified = cur.fetchone()[0] 87 cur.close() 88 89 return last_modified 90 91def lo_update_companies(data): 92 if (len(data) == 0): 93 return 94 con = s2hdb.lo_get_descriptor() 95 cur = con.cursor() 96 97 coms = [] 98 for t in data: 99 coms.append(t[0:21]) 100 #print (coms) 101 q = "INSERT INTO company (legal_name, website, `desc`, desc_jp, core_tech, core_tech_jp, development_status, development_status_jp, " + \ 102 "application, application_jp, cat_name, City, Region, Country, founded_date, funding_series, picture, CreatedDate, total_funding, com_id) VALUES " + \ 103 ", ".join(map(lambda x : "(" + ",".join(x) + ")", coms)) + " " + \ 104 "ON DUPLICATE KEY UPDATE legal_name = VALUES(legal_name)" 105 print (q) 106 cur.execute(q) 107 108 q = "INSERT INTO company (headquarter) " + \ 109 "SELECT GROUP_CONCAT(City, Region, Country SEPARATOR ', ') FROM company GROUP BY legal_name " 110 cur.execute(q) 111 con.commit() 112 cur.close() 113 114
回答1件
あなたの回答
tips
プレビュー