擅长:python、mysql、java
<p>在没有逗号的情况下,行</p>
<pre><code>loc = rep.split(' ')[-1].strip()
uni = rep.split(' ').index(loc)
</code></pre>
<p>首先找到位置作为字符串的最后一个元素,然后告诉您字符串出现在哪个索引处。你想要的是字符串中的最后一个字以外的所有东西</p>
<pre><code>uni = ' '.join(rep.split()[:-1])
</code></pre>
<p>最好先用“”替换,这样就只有一个案例需要处理。而且,我倾向于只把绳子劈开一次。你知道吗</p>
<pre><code>words = rep.split() # the default is to split at whitespace
loc = words[-1]
uni = ' '.join(words[:-1])
</code></pre>
<p>所以,我会这样写循环:</p>
<pre><code>for l in content:
rep = l.strip('"').replace(',','')
words = rep.split()
loc = words[-1]
uni = ' '.join(words[:-1])
print(uni, loc)
</code></pre>
<p>这个指纹</p>
<pre><code>('Imperial College of Business Studies', 'Lahore')
('Government College University', 'Faisalabad')
('Imperial College of Business Studies', 'Lahore')
('University of Peshawar', 'Peshawar')
('University of Sindh', 'Jamshoro')
</code></pre>
<p>我想这就是你想要的。你知道吗</p>