有人可以帮助我
从此python3列表中:
['{', ' "100": {', ' "AT": {', ' "av": -64.519, ', ' "ct": 17754, ', ' "mn": -96.023, ', ' "mx": -14.294', ' }, ', ' "First_UTC": "2019-03-08T23:09:35Z", ']
我需要列出此列表:
[100,-64.519,17754,-96.023,14.294,2019-03-08T23:09:35Z]
答案 0 :(得分:0)
这应该是您所需要的最接近的。尽可能将值转换为float
,并将其余值转换为datetime
对象
import re
import datetime
res=[]
a=['{', ' "100": {', ' "AT": {', ' "av": -64.519, ', ' "ct": 17754, ', ' "mn": -96.023, ', ' "mx": -14.294', ' }, ', ' "First_UTC": "2019-03-08T23:09:35Z", ']
for i in a:
m=re.search('([-]?\d{1,}[.\d]?[\w:-]*)',i)
if m:
try:
tmp = float(m.group(0))
except:
tmp = datetime.datetime.strptime(m.group(0), '%Y-%m-%dT%H:%M:%SZ')
res.append(tmp)
输出
[100.0,
-64.519,
17754.0,
-96.023,
-14.294,
datetime.datetime(2019, 3, 8, 23, 9, 35)]
答案 1 :(得分:-1)
您的输入:
l=['{', ' "100": {', ' "AT": {', ' "av": -64.519, ', ' "ct": 17754, ', ' "mn": -96.023, ', ' "mx": -14.294', ' }, ', ' "First_UTC": "2019-03-08T23:09:35Z", ']
_
分两个步骤:
l1= [re.sub("[^0-9-]", "", x) for x in l]
现在,您可以删除空文字并将其全部作为字符串获取。
l2=[x for x in l1 if x!=""]
您还可以将其转换为int,除了最后一个,因为“ 2019-03-08T23:09:35Z”不是有效的文字
l3 = [int(x) for x in l2[:-1]]
print(l3)
[100, -64519, 17754, -96023, -14294]