擅长:python、mysql、java
<p>使用<code>.get()</code>而不是<code>[]</code></p>
<pre><code>url = 'http://www.places4two.de/location/'+locname+'/'+str(lid)+'/'
if comment_dict.get(url).get('comments').get('data'):
#my code.
</code></pre>
<p>要安全地执行此操作(不要遇到<code>NoneType has no attribute</code>问题),还可以执行以下操作:</p>
<pre><code>url = 'http://www.places4two.de/location/'+locname+'/'+str(lid)+'/'
if comment_dict.get(url, {}).get('comments', {}).get('data', None):
#my code.
</code></pre>