擅长:python、mysql、java
<p>根据来自<a href="https://stackoverflow.com/users/1192111/francisco-couzo">Francisco Couzo</a>的答案,<a href="https://docs.python.org/3.6/library/functions.html#complex" rel="nofollow noreferrer">documentation</a>声明</p>
<blockquote>
<p>When converting from a string, the string must not contain whitespace around the central + or - operator. For example, complex('1+2j') is fine, but complex('1 + 2j') raises ValueError.</p>
</blockquote>
<p>删除字符串中的所有空格,然后就可以完成了,这段代码对我很有用:</p>
<pre><code>a = "3 + 3j"
a = a.replace(" ", "") # will do nothing if unneeded
b = complex(a)
</code></pre>