データベースのデータテーブルからデータを読み込んでいます。このメソッドは、データベース内のテーブルにデータがない場合に例外をスローします。データベースからの読み込み時にpythonスクリプトでエラーが発生しました。
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 > 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)>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
テーブル内のデータがないので、エラーがラインheader = lines.pop(0)
でスローされます。
空のデータ処理をここで正常に処理する方法を教えてください。
次のエラーがこのスクリプトの実行に見られている:
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
'if len(lines)== 0:... ' – smarx
コードを正しくインデントして、例外テキスト(私が推測するIndexError)を含めることができますか? pop&IndexErrorを見ている人はあなたの質問を見つけるでしょう。 – oekopez