I have a CSV file where fields are delimited with a comma. The fields of this CSV file contains the attributes of Permanent Survey Marks and I’ve created a GIS python script to convert it to a table that is stored it in our Enterprise GIS Spatial Database (SQL Server).

Within each row, there are some field’s that contain whitespace only. For example, part of a typical line shown here:

, ,N,NRM ,SP109773 , ,CK3693 , ,CK3543 ,

I discovered that these whitespace fields were being stored in the table as a string of whitespace characters. I needed to determine if each field had whitespace characters only and then make the script store these particular fields with a null value in the table. I came up with this function to do the job:

def isWhiteSpaceString(string):

    """  Check if string only contains whitespace characters.

         Input:   string value

         Output:  Returns True if string contains only whitespace
                  characters otherwise returns False

    """

    if (str == None):
        return False

    flag = 0
    for char in string:
        if char == " ":
            continue
        else:
            flag = 1
            break
    
    if flag == 0:
        # string only contains whitespace characters
        return True
    else:
        # string contains as least one alphanumeric character
        return False

# ---------------------------------------------------------------------------- #

a = 'abcdefg12345'
b = 'abcdefg12345     '
c = '     abcdefg12345'
d = '!                '
e = '                 '

print isWhiteSpaceString(a)
print isWhiteSpaceString(b)
print isWhiteSpaceString(c)
print isWhiteSpaceString(d)
print isWhiteSpaceString(e)

*** Python 2.5.1 (r251:54863, Apr 18 2007, 08:51:08) [MSC v.1310 32 bit (Intel)] on win32. ***
>>>
False
False
False
False
True
>>>

This works but I’m looking for alternative solution that might be a little more compact or even more efficient.

Dani AI

Generated

A few practical notes and a compact, robust approach.

: the original function has a bug — it tests str == None and also uses the name str, which shadows the built-in. ’s advice to trim is a good, concise option; another useful method is str.isspace() (it returns True only when the string contains one or more whitespace characters, and False for the empty string). Decide whether an empty field and None should be treated as “blank” for the database and handle those cases explicitly.

A compact predicate that treats None, empty string, and whitespace-only fields as blank:

def is_whitespace_only(s):
    if s is None:
        return True
    if s == '':
        return True
    return all(ch.isspace() for ch in s)

Recommended CSV-to-DB workflow: parse with Python’s csv module (use skipinitialspace=True to drop spaces after delimiters), normalize fields to None before insertion, and use parameterized SQL so Python None becomes SQL NULL:

import csv
with open('marks.csv', 'rb') as fh:
    reader = csv.reader(fh, skipinitialspace=True)
    for row in reader:
        norm = [None if is_whitespace_only(v) else v for v in row]
        cursor.execute("INSERT INTO marks (c1,c2,...) VALUES (?,?,...)", norm)

Notes and cautions: if the file is encoded (UTF-8) or contains non-breaking spaces, decode fields to Unicode first and normalize \u00A0 (replace with regular space) before testing. Both strip()-based checks and isspace() are O(n) in the field length; for typical CSV fields the difference is negligible. Always use parameterized queries rather than string formatting so NULLs are sent correctly to SQL Server.

Recommended Answers

All 2 Replies

str method strip() will remove leading and trailing whitespace characters. If a string only has whitespace characters, strip() will remove them all and it will evaluate False. Example:

>>> not 'abcdefg12345     '.strip()
False
>>> not '        '.strip()
True
>>>

Thank you. This is certainly more compact.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.