简体   繁体   English

Python脚本从文件读取数据并将其存储在mysql表中

[英]Python script to read data from a file and store it in mysql table

I am trying to write a python script that would read records from a text file and insert into a mysql table . 我正在尝试编写一个python脚本,该脚本将从文本文件中读取记录并插入到mysql table

For a particular time stamp, eg:(1438084440) and servername (r01vb01) - 6 column values are generated. 对于特定的时间戳,例如:(1438084440)和服务器名称(r01vb01)-生成6个列值。

I need to store these 8 entities into mysql table . 我需要将这8个实体存储到mysql table

As timestamp and servername are repeated in all 6 rows, I am unable to find a best way to extract and insert it into the table. 由于时间戳记和服务器名在所有6行中都重复,因此我找不到最佳的提取方法并将其插入表中。

The end result should be like : 最终结果应为:

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

I tried reading and storing data from text file into array matrix. 我尝试从文本文件读取数据并将其存储到数组矩阵中。 But not sure how to turn into a data base table. 但不确定如何将其转换为数据库表。

 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']   

You should create a dictionary that is keyed by servername and datetime 您应该创建一个以servernamedatetime为键的字典

Example: from collections import defaultdict 示例:从集合中导入defaultdict

lines = defaultdict(dict)
with open("file_parse.txt") as f:
    for line in f:
        parts = line.split()
        key = tuple(parts[1:3]) # unique key with servername and datetime
        lines[key][parts[0]] = parts[3]
        lines[key]["servername"] = parts[1]
        lines[key]["datetime"] = parts[3]
res = list(lines.values())
print(res)
# [
#    {'cpudile': 'NaN', 'cpunice': '0.0', 'cpunumber': '40.0', 'cpusystem': '0.0', 'cpuuser': '0.0', 'cpuwait': '0.0', 'datetime': 'NaN', 'servername': '1438084445'},
#    {'cpudile': 'NaN', 'cpunice': '0.0', 'cpunumber': '40.0', 'cpusystem': '0.0', 'cpuuser': '0.0', 'cpuwait': '0.0', 'datetime': 'NaN', 'servername': '1438084440'},
#    {'cpudile': 'NaN', 'cpunice': '0.0', 'cpunumber': '40.0', 'cpusystem': '0.0', 'cpuuser': '0.0', 'cpuwait': '0.0', 'datetime': 'NaN', 'servername': '1438084450'}
# ]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM