我试图从txt文件中提取一些信息,但在分割一行之后,我只能访问列表的第一个位置。 任何人都有一个想法为什么?
我的txt文件就像:
<?xml version='1.0' encoding='UTF-8'?>
<osm version="0.6" generator="osmconvert 0.8.4" timestamp="2017-03-06T01:59:59Z">
<bounds minlat="43.48" minlon="-79.7899999" maxlat="43.92" maxlon="-78.9999997"/>
<node id="699540" lat="43.6751621" lon="-79.361332" version="1"/>
<node id="699569" lat="43.7247576" lon="-79.3302633" version="1"/>
<node id="1497736" lat="43.731285" lon="-79.3304523" version="1"/>
<node id="1497764" lat="43.7412456" lon="-79.332082" version="1"/>
<node id="1497766" lat="43.7418685" lon="-79.3321184" version="1"/>
<node id="1497768" lat="43.7450436" lon="-79.3327357" version="1"/>
<node id="1497773" lat="43.7459924" lon="-79.3329589" version="1"/>
<node id="1497776" lat="43.747316" lon="-79.3332228" version="1"/>
<node id="1497778" lat="43.7484115" lon="-79.3333255" version="1"/>
和我的代码:
import re
contador = 0
pattern = re.compile("node")
with open('toronto1.txt') as text:
print("leu Arquivo")
with open('saida.txt', 'w') as saida:
print("criou arquivo")
for text_line in text:
comparacao = re.search(pattern, text_line)
if comparacao is not None:
node_line = text_line
split_id = re.findall(r"[\w']+", node_line)
saida.write(split_id[2]+'\n')
contador = contador + 1
print contador
saida.close()
text.close()
a = split_id[2]
print node_line
print split_id
print a
这是帮助。
答案 0 :(得分:0)
我很确定你得到一个只有一个大行的文件,并且你正在使用/ n关闭循环,所以你只得到一次。
当别人告诉你并按照预期的方式解析时。