Reputation: 4155
I have a string similar to this:
string = '(1:[0,0,0]; 2:[21,0,12])'
Except my string goes on for thousands of numbers.
... '4214:[9,93,42])'
How do I select the number, colon, and opening bracket? Like this:
'1:['
'2:['
'2831:['
'4214:['
I want to select each one of these and replace it with a new string: '('
.
Upvotes: 0
Views: 67
Reputation: 5383
Your data is already in the form of a dictioanry. Just do the following:
In [35]: xx = eval(string.replace(";", ',').replace('(', '{').replace(')', '}'))
The result is a dictionary ...
In [36]: xx.keys()
Out[36]: [1, 2]
In [37]: xx[2]
Out[37]: [21, 0, 12]
Upvotes: 1
Reputation: 8966
You could use a regular expression:
new_string = re.replace(r'[0-9]+:\[', r'\(', string)
This replaces all occurences of <number>:[
with (
.
Upvotes: 4