gpt4 book ai didi

python - 类型错误 : descriptor 'replace' requires a 'str' object but received a 'NoneType '

转载 作者:行者123 更新时间:2023-12-01 09:32:49 24 4
gpt4 key购买 nike

我正在尝试使用 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]

最佳答案

我认为元组中没有None,因此需要将其过滤掉

col = [tuple([str.replace(i, "\x1c"," ") for i in tup if pd.notnull(i)]) for tup in col]

或仅替换None值:

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]

关于python - 类型错误 : descriptor 'replace' requires a 'str' object but received a 'NoneType ' ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49811130/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com