python - 通过python脚本在MySql中插入文本数据时处理空数据

标签 python mysql csv

我正在 MySQL 中读取和存储数据,某些列数据由空值组成。例如

#"ssd","mode","tolfil","slm.dat","Avg.dat","GP","13","gpro"
#"TIMESTAMP","Reco.no","teminF","ver.ang","hori.ang","vel","sd","vel.data"

"2009-02-25 14:28:36.76", ,8.277527,0.68,0.15,0.42,762.0303,4.6801
"2009-02-25 14:28:36.8", ,8.24408,0.7,0.03,0.3,761.878,4.682412
"2009-02-25 14:29:36.88",2,8.277527,0.55,0.09,0.31,762.0018,4.680709
 "2009-02-25 14:30:36.92",3,8.277527,0.47,0.2,0.31,761.8914,4.684526

所以我得到了以下错误。

[1456411716, ' ', '8.277527', '0.68', '0.15', '0.42', '762.0303', '4.6801']

mysql.connector.errors.DatabaseError: 1265 (01000): 
Data truncated for column 'Reco.no' at row 1

我的代码在这里:

 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:]                                                                    
            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()

如果我喜欢这个

            data_buffer = [ts] + data_tmp[1:]           
            for val in data_buffer:
                if val == '':
                    val = None
                    data_buffer.append(val)  
                else:
                    data_buffer.append(float(val))



            print data_buffer

然后我得到了不同的错误

 ValueError: could not convert string to float:

实际上我知道如何处理空数据,但在上面提到的代码中我不知道在哪里告诉它当它遇到空空间时,它应该在MySql数据库中将读取和插入为NULL。

任何帮助将不胜感激。

最佳答案

我发现效果很好的答案是..

  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

关于python - 通过python脚本在MySql中插入文本数据时处理空数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40996206/

相关文章:

python - 使用 Pandas 创建绘图并直接显示与使用 Matplotlib 类似的输出

php - PDO 在 Mysql 中插入多个复选框值

mysql - SQL数据库设计-三个表: product,销售订单和采购订单-如何存储产品数量?

python - 添加一个 DataFrame 列以基于另一个列实例进行分组

c - 将 CSV 文件读取到结构数组

python - FlickrApi 异常 : Photo not found on calling photo. getSizes()

java - python-dateutil的Java版本

mysql - 记录太多,WAMP 挂起,因为将没有订单的客户导出到 CSV

python | Pandas 在多个时间间隔内丢弃值

mysql - 在同一列上使用多个 WHERE 条件进行选择