开发者

from log with regex extracted values fail correct insertion into sqlite table

开发者 https://www.devze.com 2023-01-28 22:29 出处:网络
Problem with stori开发者_Python百科ng extracted values in SQLite - How to adress or index list of values for proper insertion into table.

Problem with stori开发者_Python百科ng extracted values in SQLite - How to adress or index list of values for proper insertion into table.

The Regex works fine:

input logging data looks like:

0.0.0(06026104)

1.6.1(0.1501)(1011111000)

1.6.1*32(0.1446)(1010190800)

1.8.1(02484.825)

1.8.1*32(02449.574)

correct regex output:

06026104

0.1501

02484.796

with open("usage.log") as fp:
  for line in fp:
    match = re.match(r'(0\.0\.0|1\.6\.1|1\.8\.1)\(([0-9\.]+)', line)
    if not match: continue
    version, value = match.groups()
    mylist.append(value)
for item in mylist:
   t = (item)
   cursor.execute('INSERT INTO energielog(sernr) values (?)', [t])
   cursor.execute('INSERT INTO energielog(peak) values (?)', [t])
   cursor.execute('INSERT INTO energielog(kwh) values (?)', [t])

How can I achieve the proper line by line read into SQLite?

Now each line gets fed into table wrong: 06026104 is in line 1 row 1 and line 2 row 2 and line 3 row 3. Second value is only in line 4 row 1 and line 5 row 2 so forth..

For example, if I let print t[0:3], the output is:

060

0.01

024

did it make rows out of it? How can I feed value 1 (06026104) into table row "sernr" and value2 (0.1501) into table row "peak" ..


for line in fp:
        match = re.search(r'(0\.0\.0|1\.6\.1|1\.8\.1)\(([0-9\.]+)', line)
        if match: 
            version,value = match.groups() 
            extrakt.append(value)
cursor.execute('INSERT INTO energielog (sernr, peak, kwh) values (?, ?, ?)', extrakt)

that works out

0

精彩评论

暂无评论...
验证码 换一张
取 消