我试图清除一个列表,该列表实质上是我使用SQLAlchemy从Teradata DB中提取的表定义。我本质上是在尝试从字符串
替换\ r字符我尝试在如下所示的for循环中使用replace函数
import teradata
import sqlalchemy
import string
eng = sqlalchemy.create_engine('teradatasql:///?user=xxxx&
# execute sql
query = 'SHOW TABLE DBADMIN_BKP.LIKP_BKP'
result = eng.execute(query)
results = result.fetchall()
results = [items.replace("\n", "") for items in results]
print (results)
Traceback (most recent call last):
File "SQL_ALCHEMY.py", line 12, in <module>
results = [items.str_replace("\n", "") for items in results]
File "SQL_ALCHEMY.py", line 12, in <listcomp>
results = [items.str_replace("\n", "") for items in results]
AttributeError: Could not locate column in row for column 'replace'
答案 0 :(得分:1)
您的代码有一个简单的修复程序,可以使其正常运行。我敢肯定,您只是在代码中误用了.replace()
函数。这是正确的代码:
import teradata
import sqlalchemy
import string
eng = sqlalchemy.create_engine('teradatasql:///?user=xxxx&')
# execute sql
query = 'SHOW TABLE DBADMIN_BKP.LIKP_BKP'
result = eng.execute(query)
results = result.fetchall()
results = results.replace("\n", "") #This is the segment of the code that was causing the error.
print (results)
答案 1 :(得分:0)
列表中包含的项目是一个元组。
如果打印结果[0],您将得到:
(method) Injector.get(token: any, notFoundValue?: any)
现在您已经说过将其转换为字符串,我认为您是通过执行str(results [i])来完成的。这将产生:
notFoundValue
当将其转换为字符串时,Python希望确保实际上会打印“ \ r” ,而不是转义序列“ \ r”的含义,后者是回车。为此,添加了另一个反斜杠,以给出转义序列“ \\”,该转义序列实际上表示单个“ \”
Injector.get
因此,要删除此“ \\ r”字符,您需要正确识别它。