I am writing/storing data in MySql via python. If columns data in MySql contains "NAN" then how can I handle them. At the moment I know how to deal with empty spaces or None values but here I got stuck. the data looks like this
# file no 2
# some description line
#"Time","col.no","dat.va","all_val","graph_da","tem","vel","acc", "all.valu","rand"
"2016-02-25 14:50:00",0,12.94,20.88,17.87,"NAN",14,14,13.9,"NAN"
"2016-02-25 15:00:00",1,13.48,20.86,18,"NAN",13.4,13,13.2,"NAN"
"2016-02-25 15:10:00",2,13.89,20.84,18.07,18.03,12.8,12,12.6,13
"2016-02-25 21:30:00",3,15.76,20.18,17.96,18.25,0.09,0,0.07,0.16
"2016-02-25 21:40:00",4,15.72,,17.91,,0.08,"NAN",0.09,0.18
"2016-02-25 21:50:00",5,15.66,20.07,17.86,18.17,0.03,"NAN",0.05,0.09
"2016-02-25 23:30:00",6,14.98,19.26,17.12,17.44,"NAN","NAN","NAN","NAN"
there are several thousands line which includes randomly this "NAN" therefore It would be great if somebody give me logic to deal, e.g whenever it comes across NAN, it put NAN or none. my code to deal with empty spaces and None is like this.
with open(filepath) as f:
lines = f.readlines()
max_lines = len(lines)
for k, line in enumerate(lines):
if k >= (int(skip_header_line) + int(index_line_number)):
data_tmp = line.strip().split(',')
strDate = data_tmp[0].replace("\"", "")
strDate = strDate.split('.')[0]
timestamp = datetime.datetime.strptime(strDate, '%Y-%m-%d %H:%M:%S')
ts = calendar.timegm(timestamp.timetuple())
# _ts = ts * 1000000
data_buffer = [ts] + data_tmp[1:]
data_buffer = [ts] + data_tmp[1:]
data_buffer = [v if v is not "" and v is not " " else None for v in data_buffer]
print data_buffer
cursor.execute(add_data, data_buffer)
cnx.commit()
with open(marker_file, "w") as f:
f.write(" ".join([ str(item[0]), str(data_tmp[0]), str(max_lines),
str(k-int(skip_header_line)+1) ]))
cursor.close()
cnx.close()
Any help or tip would be greatly appreciated because I try my best to find the solution but I found nothing useful . Thanx in advance for help.