使用python将文本块解析为数据库字段

时间:2017-07-10 16:46:19

标签: python sql parsing

我将以下文本块存储在单个LONGTEXT数据库字段中,需要使用python脚本将其解析为另一个表。每个块具有可变数量的行,并且可以通过从END字段中减去BEGIN字段来删除顶部和底部的不必要文本。另外,' name'在插入之前需要用另一个表中的ID替换。作为python的新手,最简单的方法是什么?谢谢: - )

BEGIN字段

show 
x1b[Kadmin@testbox> show counters
Global counters:
Elapsed time since last sampling: 304.308 seconds

--------------------------------------------------------------------------------
Total tests shown: 0
--------------------------------------------------------------------------------

x1b[2048;1Hx1b[Kx1b[?1lx1b>admin@testbox>

结束字段

show 
x1b[Kadmin@testbox> show counters
Global counters:
Elapsed time since last sampling: 304.308 seconds

name                                   value     rate
--------------------------------------------------------------------------------
packet_sent                          1234456    29305
packet_received                      1234758    29305
packet_allocated                     1234826    29288
packet_lost                                0        0
--------------------------------------------------------------------------------
Total tests shown: 4
--------------------------------------------------------------------------------

x1b[2048;1Hx1b[Kx1b[?1lx1b>admin@testbox>
到目前为止

代码

import mysqldb
import difflib
from difflib_data import *

# Connect to database
db = db.connect("10.2.1.39","username","password","TEST_DATA")

def main():
     # Read TEST_RUN_ID
     trid = cursor.execute("""select TEST_RUN_ID from PERFORMANCE2""")
     for row in trid

     # Read START
     start = cursor.execute("""select START from PERFORMANCE2""")
     for row in start

     # Read END
     end = cursor.execute("""select END from PERFORMANCE2""")
     for row in end

     # Detect change between start and end
     d = difflib.Differ()
     modified = d.compare(start_lines, end_lines)

     # Parse into lines?
     for line in modified
        counterstring =
        trid =
        value = parselist[1]
        rate = parselist[2]
        tstamp =
             line 9 position 34,37
             modified.read()

     # Substitute counter id for counter string
     counterid = cursor.execute("""select COUNTER_ID from COUNTER_STRINGS where NAME = %counterstring""")

# Insert into COUNTER_DATA
try:
     cursor.execute("""INSERT INTO COUNTER_DATA VALUES (counterid, trid, value, rate, tstamp)""")
     db.commit()
except:
     db.rollback()

db.close()

0 个答案:

没有答案