我正在尝试为 json 文件制作类似 Explode 函数的东西。循环应该逐行获取一个 json 文件,并且在每一行中我有多个值,我想从该行中提取并将其与主行放在一起(如 SQL 中的横向视图或 Explode 函数)
数据看起来像这样
{"wl_id":0,"wl_customer_id":0,"wl_webpage_name":"webpage#00","wl_timestamp":"2013-01-27 16:07:02","wl_key2":103717,"wl_key3":589101,"wl_key4":23095,"wl_key5":200527,"wl_key6":60319}
现在我想要的是 SQL 爆炸这个
{"wl_id":0,"wl_customer_id":0,"wl_webpage_name":"webpage#00","wl_timestamp":"2013-01-27 16:07:02","wl_key2":103717}
{"wl_id":0,"wl_customer_id":0,"wl_webpage_name":"webpage#00","wl_timestamp":"2013-01-27 16:07:02","wl_key3":589101}
{"wl_id":0,"wl_customer_id":0,"wl_webpage_name":"webpage#00","wl_timestamp":"2013-01-27 16:07:02","wl_key4":23095}
{"wl_id":0,"wl_customer_id":0,"wl_webpage_name":"webpage#00","wl_timestamp":"2013-01-27 16:07:02","wl_key5":200527}
import io
import sys
import re
i = 0
with io.open('lateral_result.json', 'w', encoding="utf-8") as f, io.open('lat.json', encoding="utf-8") as g:
for line in g:
x = re.search('(.*wl_timestamp":"[^"]+",)', line)
y = re.search('("wl_key[^,]+),', line)
for y in line:
i = i + 1
print (x.group(0), y.group(i),'}', file=f)
我总是得到一个错误,我无法将 str 作为组,但是当我将正则表达式放在下一个 for 循环中时,它只会让我得到第一个结果并且什么都不做,或者以另一种方式它只需要相同的结果并写入它经常在行中找到一个字符。