擅长:python、mysql、java
<p>这有点罗嗦,但似乎符合您的要求:</p>
<pre><code>#!/usr/local/cpython-3.3/bin/python
import shlex
import collections
def strip_single_quote(string):
assert string[0] == "'"
assert string[-1] == "'"
return string[1:-1]
def dict_to_tuples(string):
list_ = list(shlex.shlex(string))
assert list_[0] == '{'
assert list_[-1] == '}'
list_ = list_[1:-1]
print(list_)
len_list = len(list_)
for base_index in range(0, len_list, 4):
assert list_[base_index + 1] == ':'
if list_[base_index + 3:]:
assert list_[base_index + 3] == ','
key = strip_single_quote(list_[base_index + 0])
value = strip_single_quote(list_[base_index + 2])
yield (key, value)
def main():
string = "{'InventoryTake':'Key','OtherSceneNode':'Shed','AddOtherScene':'ShedOpen'}"
od = collections.OrderedDict(dict_to_tuples(string))
print(od)
</code></pre>