python script encountering error when reading from database and data is empty
<p>I am reading data from data table in database. The method is throwing an exception when there is no data in table in database. </p>

<pre><code>def readData(table):
"""Read the database table, parse it according to the field map, and return list of parsed lines"""
cmd = GETTABLE + ' ' + table
records = []
try:
    result = subprocess.Popen(cmd, shell=True, stdout=subprocess.PIPE)
    data = result.communicate()[0]
except subprocess.CalledProcessError, e:
    errorExit( 'Unable to read ' + table + ': ' + str(e))
if (result.returncode &gt; 0):
    errorExit( 'Unable to read ' + table + ': ' + data)

lines = data.split('\n')
# Get rid of all the blank lines
lines = [line for line in lines if len(line)&gt;0]
# Pop the first line of the data and use it to parse the column names into a map
header = lines.pop(0)
fieldmap = createFieldMap(header)

for line in lines:
    records.append(getFieldMap(line, fieldmap))
return records
</code></pre>

<p><strong>The error is thrown at line <code>header = lines.pop(0)</code> since there is no data in the table.</strong></p>

<p>Kindly suggest me how to handle empty data handling over here gracefully.</p>

<p>Following error is seen on execution of this script:</p>

<blockquote>
  <p>Traceback (most recent call last): 
  File "./dumpLineSegments", line
  204, in  gnrMemberMap, gnrGroupMap =
  getGenericMemberMap('PPWR_GNR_MEM') File "./dumpLineSegments", line
  119, in getGenericMemberMap for gnrMember in readData(tablename): File
  "./dumpLineSegments", line 78, in readData header = lines.pop(0)
  IndexError: pop from empty list</p>
</blockquote>

<p>First I would edit the code in a way that indentation is preserved and show the error message. As in the comment by smarx given you just need to check if you have something in lines left to pop. But the next lines are also error prone and should handle problems. Eg. if a line is there, but it is not a legal header, this could issues a warning (in my example). However raising some meaningful exception is also a good idea.
But it is also a good idea to check for lines that are empty except for white space. The second part of the code gives then:</p>

<pre><code>lines = data.split('\n')
# Get rid of all the blank lines, stripping whitespace. Test for
# an empty string can be done as if line
lines = [line.strip() for line in lines if line.strip()]
# Pop the first line of the data and use it to parse the column names into a map
if lines: # check if you have any lines
    header = lines.pop(0)
    try: # I guess you parse the header somehow, which can fail
         fieldmap = createFieldMap(header)
    except FieldmapError: # or whatever error is raised when the parsing of the header fails
         warning.warn('{} is not a valid header'.format(header))
         return None
    else:
        for line in lines:
            records.append(getFieldMap(line, fieldmap))
        return records
else:
    return None
</code></pre>

<p>If you have no data, None is silently returned, in case of a wrong header a warning is raised.
However, if possible I would query the database with a python database driver instead of a system call.      </p>

python