我在CSV文件的最后几行中遇到了不需要的NULL的问题。例如,一个2列的CSV文件,其中有10行正确的数据&另外5行我认为是空白单元格。见txt插入:-
a8,06/05/2020
a9,06/05/2020
a10,06/05/2020
,
,
,
,
,
我使用CX_ORACLE将数据导入到数据库中,但是它也导入了5行带NULL的空白行。
有什么办法能让我在文件的最后排除CSV的空白行。下面是我导入的摘要:-
# Initialize list that will serve as a container for bind values
L = []
UpdateLog ('Load CSV file into List...')
reader = csv.reader(open(map_drive + ':\\'+ sp_file), delimiter=',')
# the code exludes the column headers on row
UpdateLog ('Skip Header Row...')
next(reader)
for row in reader:
L.append(tuple(row))
# prepare insert statement
UpdateLog ('Creating INSERT Statement')
cursor.prepare(insert)
# execute insert with executemany
cursor.executemany(None, L)
# report number of inserted rows
UpdateLog ('Inserted: ' + str(cursor.rowcount) + ' rows into ' + table)
tmpRowCount = cursor.rowcount
cursor = connection.cursor()
#commit
connection.commit()
UpdateLog ('End import to '+ table)
可能有更好的解决方案,但这是我如何解决这个问题。
for row in reader:
if any(row): # Pick up the non-blank row of list
print (row) # Just for verification
user_list.append(row) # Compose all the rest data into the list