四组数据转换为json 1-2=3.4=0 6-7=8=0 10-11=12=13 14-15=16=0的不同格式
`1-2=3.4=0` `6-7=8=0` `10-11=12=13`
a:, "1-2" a:,"6-7" a:,"10-11"
b:, "3.4" b:,"8" b:,"12"
c:, "0" c:,"0" c:,"13"原始数据
x = {'OUT': '091309@@@;1-2=3.4=0;6-7=8=0;10-11=12=13;14-15=16=0'}python代码
import json
x = json.dumps(x)
tmp = json.loads(x)
print(tmp.keys())检索密钥
tmp['OUT']
'091309@@@;1-2=3.4=0;6-7=8=0;10-11=12=13;14-15=16=0'预期结果
x = {'OUT':"091309@@@{'a': '1-2','b': '3.4','c': '0'},{'a': '6-7','b': '8','c': '0'},{'a': '10-11','b': '12','c': '13'},{'a': '14-15','b': '16','c': '0'}"}发布于 2021-07-14 04:39:05
试试regex
import re
import json
x = {'OUT': '091309@@@;1-2=3.4=0;6-7=8=0;10-11=12=13;14-15=16=0'}
parsed_data = re.findall(r'([\d.]+-[\d.]+)=([\d.]+)=([\d.]+)', x['OUT'])
x['OUT'] = x['OUT'].split(';')[0] + ','.join(str(dict(zip(['a', 'b', 'c'], i))) for i in parsed_data)
x{'OUT': "091309@@@{'a': '1-2', 'b': '3.4', 'c': '0'},{'a': '6-7', 'b': '8', 'c': '0'},{'a': '10-11', 'b': '12', 'c': '13'},{'a': '14-15', 'b': '16', 'c': '0'}"}regex发现这里的详细信息
发布于 2021-07-14 04:32:49
IIUC,以下是将原始数据转换为预期结果的一种方法:
x['OUT'] = x['OUT'].split(';')[0] + ','.join(
str(dict(zip(['a', 'b', 'c'], i.split('=')))) for i in x['OUT'].split(';')[1:])https://stackoverflow.com/questions/68371909
复制相似问题