擅长:python、mysql、java
<p>正则表达式是非常强大和有用的,但它需要花很多时间来适应使用他们与一些权威。我建议您坚持使用split,这里是split的帮助信息,它描述了如何使用maxsplit值来限制split的数量。你知道吗</p>
<pre><code>Help on built-in function split:
split(...)
S.split([sep [,maxsplit]]) -> list of strings
Return a list of the words in the string S, using sep as the
delimiter string. If maxsplit is given, at most maxsplit
splits are done. If sep is not specified or is None, any
whitespace string is a separator and empty strings are removed
from the result.
</code></pre>
<p>所以对于你的代码,假设你有一些行要分割</p>
<pre><code>mytest = dict()
for each_line in data:
number, name = line.split(None,1)
mytest[number] = name
</code></pre>
<p>会有这样的回报吗</p>
<blockquote>
<blockquote>
<blockquote>
<p>mytest
{'27': 'anjou pear', '7645': 'langsat', 'number': 'name', '36': 'asian pear', '14': 'apple'}
to access the help suppose you have some string mystring then just type</p>
</blockquote>
</blockquote>
</blockquote>
<pre><code>help(mystring.split)
</code></pre>
<p>我第一次尝试和这次尝试的不同是因为下面的评论。在我的第一次尝试中,name值上的前导空格被保留,但是,通过使用None,在第一次拆分时所有的空格字符都被删除了,这样就可以更具体地了解您要查找的内容。你知道吗</p>