1
I’m trying to make an intersection between two arrays using the lib re
, but is returning me the following output:
OUTPUT:
selectobj = filter(regex.search, result_psql) Typeerror: expected string or buffer
Explaining script:
diff_array
= These are the elements that should be compared, it is an array generated within the script, with the function below:
diff_array = []
def diff(first, second):
second = set(second)
return [item for item in first if item not in second]
result_psql
= elements are a select in the PSQL database
result_psql = cursorpost.fetchall()
SCRIPT:
for element in diff_array:
regex = re.compile(r'.*'+element+'')
selectobj = filter(regex.search, result_psql)
print(selectobj)
GOAL:
I want to make an intersection between two arrays, where the elements that are similar should be returned, just as it is done with the LIKE of SQL
Luis, you don’t specify which library you’re using to connect to the database, but I’m going to assume that this library PEP 249 that normalizes database Apis. You will see which method
Cursor.fetchall()
returns a sequence of sequences. Knowing this, you will see that the methodRegExp.search()
takes a string as first argument.. So it is likely that your error is this.– fernandosavio
And also, your method
diff
could use sets if the order does not matter.. The whole method could be replaced byset(first) & set(second)
... It would be more performatic.– fernandosavio
@fernandosavio estou utilizando os seguintes Imports: requests, json, sys, psycopg2, re, datetime e re
– Luis Henrique
I must pass to Regexp.search() as a string ?
– Luis Henrique