Python Forum
[SOLVED] [sqilte3] Check if column not empty?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[SOLVED] [sqilte3] Check if column not empty?
#1
Hello,

Some rows might have an empty column.

How can I check it's filled with data?

file = open("output.txt", 'at',encoding='utf-8')
for row in cur.execute("select id,introtext,fulltext from content"):
	soup = BeautifulSoup(row["introtext"], 'lxml')
	file.write(soup.body.text)
	
	#Not null?
	#AttributeError: 'NoneType' object has no attribute 'text'
	#if row["fulltext"]:
	if len(row["fulltext"]):
		soup = BeautifulSoup(row["fulltext"], 'lxml')
		file.write(soup.body.text)
file.close()
Thank you.
Reply
#2
Untested, but what about checking that row returns True?

for row in cur.execute("select id,introtext,fulltext from content"):
    if row:
        soup = BeautifulSoup(row["introtext"], 'lxml')
        file.write(soup.body.text)
Sig:
>>> import this

The UNIX philosophy: "Do one thing, and do it well."

"The danger of computers becoming like humans is not as great as the danger of humans becoming like computers." :~ Konrad Zuse

"Everything should be made as simple as possible, but not simpler." :~ Albert Einstein
Reply
#3
Checking "if row:" is useless. If row was falsey, you wouldn't be in the body of the loop and you couldn't perform the test. If you are performing the test you know that row is not falsey, so performing the test will be truey.

The solution depends on what you want to do. If all you care about is fulltext, you could write a check into your query.
for row in cur.exececute("SELECT fulltext FROM content WHERE fulltext <> '' AND fulltext IS NOT NULL"):
Now, if you are in the body of the loop, you know that fulltext is not empty.

But your query selects id, introtext and fulltext, which probably means you want the query to return introtext even if fulltext is empty. In this case you have to test the retuned values.
if row["fulltext"]:
    do_something_with_full_text(row["fulltext"])
Reply
#4
You can check if the column is filled with data by using the built-in python function len() to check the length of the column value. If the length is greater than zero, it means that the column is not empty and has data.

In the current code, you can see that the if statement if len(row["fulltext"]): is checking whether the fulltext column is filled with data before parsing it with BeautifulSoup and writing it to the output file.

You can also use if row["fulltext"] is not None: or if row["fulltext"] != None: or if row["fulltext"] != "": or if bool(row["fulltext"])

Note that if the column is filled with a whitespace or any whitespace characters, the above conditions will still consider it as filled.

Option 1: you can use the len() function to check if the fulltext column is filled with data before parsing it with BeautifulSoup

file = open("output.txt", 'at',encoding='utf-8')
for row in cur.execute("select id,introtext,fulltext from content"):
soup = BeautifulSoup(row["introtext"], 'lxml')
file.write(soup.body.text)
if len(row["fulltext"]) > 0:
soup = BeautifulSoup(row["fulltext"], 'lxml')
file.write(soup.body.text)
file.close()


Option 2: you can use if row["fulltext"] is not None

file = open("output.txt", 'at',encoding='utf-8')
for row in cur.execute("select id,introtext,fulltext from content"):
soup = BeautifulSoup(row["introtext"], 'lxml')
file.write(soup.body.text)
if row["fulltext"] is not None:
soup = BeautifulSoup(row["fulltext"], 'lxml')
file.write(soup.body.text)
file.close()
Reply
#5
Thanks everyone.

It turns out that in some rows, the "fulltext" column holds either just a space or carriage return (can't tell on the CLI). As a result, sqlite considers it's not empty… but BS isn't happy since it's not legit HTML:

#Not null?
#BAD if row["fulltext"]:
#BAD if len(row["fulltext"]):
#AttributeError: 'NoneType' object has no attribute 'text'
#if row["fulltext"] is not None:
#AttributeError: 'NoneType' object has no attribute 'text'
#fulltext holds just an empty space or carriage return?
#| |
#sqlite> select fulltext from content where id=117;
if len(row["fulltext"]) > 0:
	print("FULL",row["fulltext"])
	soup = BeautifulSoup(row["fulltext"], 'lxml')
	#file.write(soup.prettify()+"\n")	
	file.write(soup.body.text)
Reply
#6
Found it: It contained a space

sqlite> select hex(fulltext) from content where id=117;
20


if row["fulltext"] and not row["fulltext"].isspace():
	soup = BeautifulSoup(row["fulltext"], 'lxml')
	file.write(soup.body.text)
Thank you.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  [solved] list content check paul18fr 6 677 Jan-04-2024, 11:32 AM
Last Post: deanhystad
  Delete empty text files [SOLVED] AlphaInc 5 1,543 Jul-09-2022, 02:15 PM
Last Post: DeaD_EyE
  Check if clients are online with ips stored in json [SOLVED] AlphaInc 6 2,450 Jun-27-2022, 08:28 AM
Last Post: AlphaInc
  [Solved] Reading every nth line into a column from txt file Laplace12 7 5,210 Jun-29-2021, 09:17 AM
Last Post: Laplace12
  Python Openpyxl is unable to check from Column 6 onwards Skye 0 1,714 Oct-13-2020, 06:11 AM
Last Post: Skye
  write to excel will be empty in column jacklee26 7 3,342 Jun-27-2020, 12:09 AM
Last Post: snippsat
  Check for a special characters in a column and flag it ayomayam 0 2,039 Feb-12-2020, 03:04 PM
Last Post: ayomayam
  How to get previous non empty value of another column klllmmm 1 1,793 Feb-02-2020, 12:22 PM
Last Post: klllmmm
  find empty cells in a column Pedroski55 2 23,801 Sep-18-2017, 01:27 PM
Last Post: Pedroski55
  How to check if the data is empty in a database? chris0147 8 20,175 Aug-28-2017, 12:03 AM
Last Post: Fran_3

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020