擅长:python、mysql、java
<p>我认为您有一个文件<code>D:\listfile</code>,其中的行包含python列表,例如</p>
<pre><code>['Mr David',23,'City']
</code></pre>
<p>你想读入一个列表。但是,当您在文件上循环时,每一行都被读取为一个字符串。所以<code>ch</code>是一个字符串,但您希望它是一个列表。你知道吗</p>
<p>如果您相信列表文件的内容包含安全表达式,那么可以让python对字符串求值</p>
<pre><code>ListNow.append(eval(ch))
</code></pre>
<p>如果列表文件不受信任(例如,如果它包含从网站收集的数据),这是危险的,因为会运行列表文件中的恶意代码。在这种情况下,您必须分析字符串,从分体式(',')</p>