含重复POWERON标记的CSV文件导入SQLite如何补全时间字段
问题描述
现有CSV日志格式
# time;P_O2, atm;P_He, atm;Flow O2, l/min;Flow He, l/min;FiO2 Env, %;FiO2 sens2, %;P mask, cm H2O;Tmask, gradC;Tnagr, gradC;V, ml;f, 1/min;Tzad, gradC;FiO2 zad, %;Flags; # POWERON 01.11.2018 15:02:29 1;39;33;0;2;0;0;255;135;135;0;0;0;25;83886592;0 5;39;33;0;2;0;0;255;135;135;0;0;0;25;83886624;0 26;0;0;0;8;529;0;255;135;135;0;0;0;25;83886592;0 72;0;0;0;8;598;0;248;135;135;0;0;0;25;83886085;0 # POWERON 01.11.2018 15:04:02 1;0;0;0;7;0;0;255;135;135;0;0;0;25;83886592;0 2;0;0;0;7;113;0;255;135;135;0;0;0;25;83886085;0 # POWERON 01.11.2018 15:04:48 1;0;0;0;6;0;0;255;135;135;0;0;0;25;83886592;0 2;0;0;0;6;115;0;255;135;135;0;0;0;25;83886085;0
当前导入代码
import sqlite3 import pandas as pd conn = sqlite3.connect('mydb.db') stud_data = pd.read_csv(r'Log/20181101.LOG', sep=';', engine='python') stud_data.to_sql('interation', conn, if_exists='replace', index=False) cur = conn.cursor() for row in cur.execute('SELECT * FROM interation'): print(row) conn.close()
当前导入结果
('# POWERON 01.11.2018 15:02:29', None, None, None, None, None, None, None, None, None, None, None, None, None, None, None) ('1', 39.0, 33.0, 0.0, 2.0, 0.0, 0.0, 255.0, 135.0, 135.0, 0.0, 0.0, 0.0, 25.0, 83886592.0, 0.0) ('5', 39.0, 33.0, 0.0, 2.0, 0.0, 0.0, 255.0, 135.0, 135.0, 0.0, 0.0, 0.0, 25.0, 83886624.0, 0.0) ('26', 0.0, 0.0, 0.0, 8.0, 529.0, 0.0, 255.0, 135.0, 135.0, 0.0, 0.0, 0.0, 25.0, 83886592.0, 0.0) ('72', 0.0, 0.0, 0.0, 8.0, 598.0, 0.0, 248.0, 135.0, 135.0, 0.0, 0.0, 0.0, 25.0, 83886085.0, 0.0) ('# POWERON 01.11.2018 15:04:02', None, None, None, None, None, None, None, None, None, None, None, None, None, None, None) ('1', 0.0, 0.0, 0.0, 7.0, 0.0, 0.0, 255.0, 135.0, 135.0, 0.0, 0.0, 0.0, 25.0, 83886592.0, 0.0) ('2', 0.0, 0.0, 0.0, 7.0, 113.0, 0.0, 255.0, 135.0, 135.0, 0.0, 0.0, 0.0, 25.0, 83886085.0, 0.0) ('# POWERON 01.11.2018 15:04:48', None, None, None, None, None, None, None, None, None, None, None, None, None, None, None) ('1', 0.0, 0.0, 0.0, 6.0, 0.0, 0.0, 255.0, 135.0, 135.0, 0.0, 0.0, 0.0, 25.0, 83886592.0, 0.0)
需求
将POWERON行中的日期值作为新增的POWERON列,填充到对应分段下的所有数据行中,预期效果如下:
POWERON; time; P_O2, atm; P_He, atm; .... 01.11.2018 15:02:29 1; 39; 33; ... 01.11.2018 15:02:29 5; 39; 33; ...
解决方案
这个需求无法仅通过pd.read_csv的参数直接实现,需要先对原始日志的行做预处理,再生成DataFrame存入数据库,实现代码如下:
import sqlite3 import pandas as pd # 1. 读取日志所有行,先处理表头和POWERON标记 log_path = r'Log/20181101.LOG' with open(log_path, 'r', encoding='utf-8') as f: lines = f.readlines() # 2. 提取表头:第一行#后的内容为原始列名 headers = [h.strip() for h in lines[0].lstrip('#').strip().split(';') if h.strip()] # 新增POWERON列到表头最前面 headers.insert(0, 'POWERON') # 3. 逐行处理数据,匹配POWERON时间到对应数据行 processed_data = [] current_poweron_time = None for line in lines[1:]: line = line.strip() if not line: continue # 识别POWERON行,更新当前分段的时间 if line.startswith('# POWERON'): current_poweron_time = line.split(maxsplit=2)[2].strip() continue # 处理普通数据行,插入当前POWERON时间 row = [v.strip() for v in line.split(';') if v.strip()] row.insert(0, current_poweron_time) processed_data.append(row) # 4. 生成DataFrame,可根据需要转换列的数值类型 df = pd.DataFrame(processed_data, columns=headers) df = df.apply(pd.to_numeric, errors='ignore') # 5. 存入数据库 conn = sqlite3.connect('mydb.db') df.to_sql('interation', conn, if_exists='replace', index=False) # 验证结果 cur = conn.cursor() for row in cur.execute('SELECT * FROM interation LIMIT 3'): print(row) conn.close()
处理后所有数据行都会携带对应分段的POWERON时间,无需再对数据库做额外迭代处理。
内容的提问来源于stack exchange,提问作者Djalyarim
相关产品推荐
相关产品推荐

