Python: Filter out rows from result set of pyodbc.row when row contains string
Question:
I have not been able to find an answer to this seemingly straightforward filter process.
I have a result set of table names for a simple odbc query and I want to filter that result set anything that contains the prefix ‘wer_’
*Some pyodbc connection code*
cursor.execute(<SQL statement which gets the list of tables>)
results = cursor.fetchall()
results = [key for key in results if str(key.name).str.contains('wer_')]
^ I’ve tried various methods around this but so far no dice. Can you help?
Answers:
It turned out to be fairly straight forward in the end. It seems pyodbc.row has an attribute of name which you can compare against
*Some pyodbc connection code*
cursor.execute(<SQL statement which gets the list of tables>)
results = cursor.fetchall()
results = [key for key in results if 'wer_' not in key.name]
I hope this helps somebody in the future!
I have not been able to find an answer to this seemingly straightforward filter process.
I have a result set of table names for a simple odbc query and I want to filter that result set anything that contains the prefix ‘wer_’
*Some pyodbc connection code*
cursor.execute(<SQL statement which gets the list of tables>)
results = cursor.fetchall()
results = [key for key in results if str(key.name).str.contains('wer_')]
^ I’ve tried various methods around this but so far no dice. Can you help?
It turned out to be fairly straight forward in the end. It seems pyodbc.row has an attribute of name which you can compare against
*Some pyodbc connection code*
cursor.execute(<SQL statement which gets the list of tables>)
results = cursor.fetchall()
results = [key for key in results if 'wer_' not in key.name]
I hope this helps somebody in the future!