我正在尝试使用python直接从远程oracle数据库查询的元组列表中的空格(“”)替换值'\ x1c'。我收到这个错误:
TypeError: descriptor 'replace' requires a 'str' object but received a 'NoneType '
以下是我正在使用的代码,即出现错误的最后一行代码:
connection = cx_Oracle.connect("WELCOME", "welcome", "(ABC)")
cursor = connection.cursor()
querystring = "select CUST_OR_CARD_ID,MESG from tbaadm.rtt where
SYSTEM_DATE_TIME >= '01/JAN/18' and dcc_id='SPR'"
cursor.execute(querystring)
col=cursor.fetchall()
col = [tuple(map(lambda i: str.replace(i, "\x1c"," "), tup)) for tup in col]
答案 0 :(得分:1)
我认为元组中有None
个,所以需要将其过滤掉
col = [tuple([str.replace(i, "\x1c"," ") for i in tup if pd.notnull(i)]) for tup in col]
仅限replace
非None
s值:
col=[tuple([str.replace(i, "\x1c"," ") if pd.notnull(i) else i for i in tup ]) for tup in col]
编辑:感谢@Paul Cornelius建议使用string.replace
:
col = [tuple([i.replace("\x1c"," ") for i in tup if pd.notnull(i)]) for tup in col]
col = [tuple([i.replace("\x1c"," ") if pd.notnull(i) else i for i in tup ]) for tup in col]