Someone can help me :
from this python3 list :
['{', ' "100": {', ' "AT": {', ' "av": -64.519, ', ' "ct": 17754, ', ' "mn": -96.023, ', ' "mx": -14.294', ' }, ', ' "First_UTC": "2019-03-08T23:09:35Z", ']
I need to make this list :
[100,-64.519,17754,-96.023,14.294,2019-03-08T23:09:35Z]
This should be the closest what you need. Convert the values to float
where possible and convert rest of the values to datetime
objects
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)
Output
[100.0,
-64.519,
17754.0,
-96.023,
-14.294,
datetime.datetime(2019, 3, 8, 23, 9, 35)]
Your input:
l=['{', ' "100": {', ' "AT": {', ' "av": -64.519, ', ' "ct": 17754, ', ' "mn": -96.023, ', ' "mx": -14.294', ' }, ', ' "First_UTC": "2019-03-08T23:09:35Z", ']
_
in two steps:
l1= [re.sub("[^0-9-]", "", x) for x in l]
Now, you can remove empty literals and get it all as string.
l2=[x for x in l1 if x!=""]
You can also translate to int, except the last because "2019-03-08T23:09:35Z" is not a valid literal
l3 = [int(x) for x in l2[:-1]]
print(l3)
[100, -64519, 17754, -96023, -14294]
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.