KacieHouser
KacieHouser

Reputation: 2125

Python check for blank CSV value not working

I have a CSV file and I am running a script against it to insert into a database. If the value is blank then I don't want to insert it. Here is what I have

if attrs[attr] != '' and attrs[attr] != None:
            log.info('Attriute ID: %s' % attr)
            log.info('Attriute Value: %s' % attrs[attr])
            sql = insert_attr_query(attrs[attr], object_id, attr)
            cursor.execute(sql)

It's blank and it doesn't = '' or None, then wth does it =?

Upvotes: 1

Views: 2653

Answers (3)

John Machin
John Machin

Reputation: 82934

You should (almost) always normalise whitespace in any text string that is intended for insertion in a database (or for many other purposes).

To normalise whitespace is to (1) strip any leading whitespace (2) strip any trailing whitespace (3) replace any internal runs (length >= 1) of whitespace by exactly 1 SPACE (U+0020).

Whitespace should not be limited to what standard Python provides, especially if you are working in Python 2.X and not using unicode objects. For example, in the default "C" locale, "\xA0" is not treated as whitespace but it's very likely to represent NO-BREAK SPACE (U+00A0).

Sample code for Python 2.X:

def normalize_white_space_u(unicode_object):
    return u' '.join(unicode_object.split())

def normalize_white_space_s(str_object):
    return ' '.join(str_object.replace('\xA0', ' ').split())

Generalizing the second function: replace each occurrence of a non-standard whitespace character by a single space and then do the split-join dance.

Upvotes: 1

Dave Kirby
Dave Kirby

Reputation: 26552

Presumably it contains whitespace. You could check this by printing repr(attrs[attr]) which will put quotes round it and show tabs at "\t"

Change the code to if attrs[attr] is not None and attrs[attr].strip() !="":

Upvotes: 3

Martin
Martin

Reputation: 7149

It's probably whitespace i.e. a tab or string with spaces try:-

attrs[attr].strip()

Upvotes: 4

Related Questions