I have a string :
mystring = "Foo: Bar (Titi) Foo-age: 50 Airplanes: 12:1 12:3 12:4 12:5 [...] Next Hop: LAX Origine ID: 49 Hop List 2 4 9 0 3 [...]"
Is there a way to split this string using a pattern, like:
pattern = {"Foo", "Foo-age", "Airplanes", "Next Hop", "Origine ID", "Hop List"}
and then
mylist = somefunction(mystring , pattern)
print mylist
--> {"Foo":"Bar (Titi)","Foo-age" : 50, "Airplanes": ["12:1","12:3",...], ...}
Is that possible in python?
[EDIT]
Some sample data – a 5-col csv file w/ delimiter “,”
col-1,col-2,Path: 9876 (IGP) Local-Pref: 310000 MED: 0 Communities: 1234:6 1234:95 1234:101 1234:202 1234:500 1234:903 1234:3369 1234:8000 1234:8002 1234:16925 9876:19827 Next Hop: x.x.127.151 Originator ID: x.x.155.144 Cluster List: 0.0.29.99 0.0.29.97 0.0.26.245 0.0.2.179 ,col-4,col-5
col-1,col-2,Path: 9876 (IGP) Local-Pref: 310000 MED: 0 Communities: 1234:3 1234:95 1234:101 1234:202 1234:13705 9876:19941 Next Hop: x.x.127.61 Originator ID: x.x.137.37 Cluster List: 0.0.29.99 0.0.29.97 0.0.1.195 ,col-4,col-5
col-1,col-2,Path: 9876 (IGP) Local-Pref: 310000 MED: 0 Communities: 1234:2 1234:95 1234:101 Next Hop: x.x.127.149 Originator ID: x.x.137.29 Cluster List: 0.0.29.99 0.0.29.98 0.0.2.240 ,col-4,col-5
col-1,col-2,Path: 9876 (IGP) Local-Pref: 310000 MED: 0 Communities: 1234:6 1234:95 1234:101 1234:202 1234:500 1234:903 1234:3369 1234:8000 1234:8002 1234:16924 9876:19827 Next Hop: x.x.127.151 Originator ID: x.x.155.144 Cluster List: 0.0.29.99 0.0.29.97 0.0.26.245 0.0.2.179 ,col-4,col-5
I think this can be done in two steps. First, you look for something that looks like a field name (
Foo-Bar:) and insert a “special” marker character (e.g.@) before each match. Second, you look for the patternmarker field-name : dataand populate the data dictionary:Result: