'How do I handle URL validation of a list, where some rows are NULL?
I have a very large SQL table containing a list of thousands of URLs. I would like to validate whether or not these URLs are valid. The problem is that some of the rows in the table contain NULL, and this is giving me trouble when I try to use Python Validators, because I get the error "TypeError: expected string or bytes-like object"
I tried to catch the error with an exception, but for some reason this prints out a TypeError for URLs that were actually showing up as "valid" before I tried the exception. example:
If I don't use an exception it will print:
"Url is valid"
"Url is valid"
"Url is valid"
"TypeError" (Program stops)
If I use an exception it will print:
"Url is valid"
"TypeError"
"TypeError"
"TypeError"
The code looks like this
# Read from database
sql_select = "SELECT PDF_Url FROM DATA"
records = cursor_object.execute(sql_select).fetchall()
# Store data in list and validate URL
data_list = [i[0] for i in records]
for row in data_list:
try:
valid = validators.url(row)
if valid:
print("Url is valid")
else:
print("Invalid url")
except TypeError:
print("TypeError: Check list of indices")
print(data_list)
Full error:
Traceback (most recent call last):
File "C:\Users\PycharmProjects\excel\main.py", line 4, in <module>
database_connector()
File "C:\Users\PycharmProjects\excel\database_reader.py", line 36, in database_connector
valid = validators.url(row)
File "C:\Users\PycharmProjects\excel\venv\lib\site-packages\decorator.py", line 232, in fun
return caller(func, *(extras + args), **kw)
File "C:\Users\PycharmProjects\excel\venv\lib\site-packages\validators\utils.py", line 83, in wrapper
value = func(*args, **kwargs)
File "C:\Users\PycharmProjects\excel\venv\lib\site-packages\validators\url.py", line 148, in url
result = pattern.match(value)
TypeError: expected string or bytes-like object
Url is valid
Url is valid
Url is valid
Invalid url
Url is valid
Process finished with exit code 1
Solution 1:[1]
Why not prevent that None values are passed to the validator? It will save you the try/except:
for row in data_list:
if row:
valid = validators.url(row)
if valid:
print("Url is valid")
else:
print("Invalid url")
else:
print("Invalid url")
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | RJ Adriaansen |
