擅长:python、mysql、java
<pre><code>countries=[]
cities=[]
with open("countries.txt") as f:
gap=True
for line in f:
line=line.strip()
if gap:
countries.append(line)
gap=False
elif line=="":
gap=True
else:
cities.append(line)
print countries
print cities
</code></pre>
<p>输出:</p>
^{pr2}$
<p>如果要将这些写入文件:</p>
<pre><code>with open("countries.txt","w") as country_file, open("cities.txt","w") as city_file:
country_file.write("\n".join(countries))
city_file.write("\n".join(cities))
</code></pre>