我正在尝试编写一个python脚本,该脚本将从文本文件中读取记录并插入 mysql table
。
对于特定时间戳,例如:(1438084440)和servername(r01vb01) - 生成6个列值。
我需要将这8个实体存储到 mysql table
。
由于时间戳和服务器名称在所有6行中重复,我无法找到提取并将其插入表格的最佳方法。
最终结果应该是:
servername date_time cpu_number cpu_user cpu_nice cpu_system cpu_wait cpu_idle
r01vb01 1438084440 40.0 0.0 0.0 0.0 0.0 NaN
r01vb01 1438084445 40.0 0.0 0.0 0.0 0.0 NaN
r01vb01 1438084450 40.0 0.0 0.0 0.0 0.0 NaN
Table structure :
CREATE TABLE `cpu_util_all` (
`servername` VARCHAR(45) NOT NULL,
`date_time` DATETIME NOT NULL,
`cpu_number` DECIMAL(10,2) NULL,
`cpu_user` DECIMAL(10,2) NULL,
`cpu_nice` DECIMAL(10,2) NULL,
`cpu_system` DECIMAL(10,2) NULL,
'cpu_wait` DECIMAL(10,2) NULL,
`cpu_idle` DECIMAL(10,2) NULL,
PRIMARY KEY (`servername`, `date_time`));
Text file :
cpunumber 1438084440 r01vb01 40.0
cpunumber 1438084445 r01vb01 40.0
cpunumber 1438084450 r01vb01 40.0
cpunice 1438084440 r01vb01 0.0
cpunice 1438084445 r01vb01 0.0
cpunice 1438084450 r01vb01 0.0
cpusystem 1438084440 r01vb01 0.0
cpusystem 1438084445 r01vb01 0.0
cpusystem 1438084450 r01vb01 0.0
cpuwait 1438084440 r01vb01 0.0
cpuwait 1438084445 r01vb01 0.0
cpuwait 1438084450 r01vb01 0.0
cpuuser 1438084440 r01vb01 0.0
cpuuser 1438084445 r01vb01 0.0
cpuuser 1438084450 r01vb01 0.0
cpudile 1438084440 r01vb01 NaN
cpudile 1438084445 r01vb01 NaN
cpudile 1438084450 r01vb01 NaN
我尝试将文本文件中的数据读取并存储到数组矩阵中。但不确定如何变成数据库表。
with open("test.txt") as textFile:
lines = [line.split() for line in textFile]
[['cpunumber', '1438084440', 'r01vb01', '40.0'], ['cpunumber', '1438084445', 'r01vb01', '40.0'],
['cpunumber', '1438084450', 'r01vb01', '40.0'], ['cpunice', '1438084440', 'r01vb01', '0.0'],
['cpunice', '1438084445', 'r01vb01', '0.0'], ['cpunice', '1438084450', 'r01vb01', '0.0'],
['cpusystem', '1438084440', 'r01vb01', '0.0'], ['cpusystem', '1438084445', 'r01vb01', '0.0'],
['cpusystem', '1438084450', 'r01vb01', '0.0'], ['cpuwait', '1438084440', 'r01vb01', '0.0'],
['cpuwait', '1438084445', 'r01vb01', '0.0'], ['cpuwait', '1438084450', 'r01vb01', '0.0'],
['cpuuser', '1438084440', 'r01vb01', '0.0'], ['cpuuser', '1438084445', 'r01vb01', '0.0'],
['cpuuser', '1438084450', 'r01vb01', '0.0'], ['cpudile', '1438084440', 'r01vb01', 'NaN'],
['cpudile', '1438084445', 'r01vb01', 'NaN'], ['cpudile', '1438084450', 'r01vb01', 'NaN']
答案 0 :(得分:0)
You should create a dictionary that is keyed by <a href="/jpetstore/shop/viewCategory.shtml?categoryId=FISH"><img border="0" src="../images/sm_fish.gif" /></a>
and servername
Example: from collections import defaultdict
datetime