将从cqlite读入的数据写入csv有空白行

需求:读取sqlite里面的数据,找出某个字段不为3的值,将对应的信息写入csv文件。
刚开始写入怎么都会有一行空白,代码如下:

failtask_sequre_sql=”SELECT plan_file_task.TaskState,plan_file_task.CIPServerIP,plan_file_task.StartTime,plan_file_task.EndTime,plan_file_task.CreateTime
FROM plan_file_task WHERE plan_file_task.TaskState!=3 AND
plan_file_task.DeviceID=”+str(row[0])+” AND plan_file_task.StartTime
BETWEEN ‘”+starttime_contents.GetValue().strip()+”’ and
‘”+endtime_contents.GetValue().strip()+”’”
failtask_task_cursor=connection.cursor()
failtask_task_cursor.execute(failtask_sequre_sql)
failtask_results=failtask_task_cursor.fetchall()
if os.path.lexists(os.path.dirname(db_path)+’\log\’):
pass
else:
os.mkdir(os.path.dirname(db_path)+’\log\’)
with open(os.path.dirname(db_path)+’\log\’+str(row[0])+’未成功.csv’,’w’) as
failtask_csvfile:
writer = csv.writer(failtask_csvfile)
writer.writerow([“TaskState”,”CIPServerIP”,”StartTime”,”EndTime”,”CreateTime”])
writer.writerows(failtask_results)
failtask_csvfile.close
运行这段代码后怎么都是有空白。在打开csv的参数加一个, newline=”就好了。
就是将:with open(os.path.dirname(db_path)+’\log\’+str(row[0])+’未成功.csv’,’w’) as failtask_csvfile:
改为with open(os.path.dirname(db_path)+’\log\’+str(row[0])+’未成功.csv’,’w’, newline=”) as failtask_csvfile:

你可能感兴趣的:(csv,python3,python)