回答此问题可获得 20 贡献值,回答如果被采纳可获得 50 分。
<p>我正在尝试将以下字符串转换为字典,其中IP将成为密钥,而|之后的所有内容都将成为值:</p>
<pre><code>my_string = '''10.11.22.33|{"property1": "0", "property2": "1", "property3": "1", "property4": "1", "property5": "0"}
10.11.22.34|{"property1": "0", "property2": "0", "property3": "1", "property4": "1", "property5": "0", "property6": "0", "property7": "1", "property8": "0", "property9": "0", "property10": "1"}'''
</code></pre>
<p>这是我尝试过的代码:</p>
^{pr2}$
<p>但是,我得到一个错误:</p>
<pre><code>ValueError: dictionary update sequence element #1 has length 1; 2 is required
</code></pre>
<p>所以,我把我的_字符串简化为一行:</p>
<pre><code>my_string = '10.11.22.33|{"property1": "0", "property2": "1", "property3": "1", "property4": "1", "property5": "0"}'
</code></pre>
<p>并使用此代码首先拆分行:</p>
<pre><code>wow = my_string.split('|')
</code></pre>
<p>输出:</p>
<pre><code>['10.11.22.33', '{"property1": "0", "property2": "1", "property3": "1", "property4": "1", "property5": "0"}']
</code></pre>
<p>上面是两个元素的列表。但是,当我试图从中创建字典时,它失败了,并出现以下错误:</p>
<pre><code>d = dict(wow)
</code></pre>
<p>输出:</p>
<pre><code>ValueError: dictionary update sequence element #0 has length 11; 2 is required
</code></pre>
<p>我不想修改这个值-它需要保持原样。把这一行输入字典的正确方法是什么</p>
<pre><code>{'10.11.22.33': '{"property1": "0", "property2": "1", "property3": "1", "property4": "1", "property5": "0"}'}
</code></pre>
<p>这是python2.6。在</p>