with open('ch4_int_coord.txt') as f:
for line in f:
line1 = re.search(r'\w{1,2}', f)
line2 = re.search(r'\w{1,2}\s+(\d+)\s+\d+\.+\d+', int_coord)
print line1
这就是我目前所拥有的。我正在尝试为文件中的每一行使用新的正则表达式模式(因为每一行都有不同的数据量)但我不确定如何指定它。
请您参考如下方法:
您可以使用字典来保存您的正则表达式并通过简单的索引访问它们,可以将相对行数作为键并使用 enumerate 函数遍历文件对象以访问到行索引。
regex_dict={1:r'\w{1,2}',2:r'\w{1,2}\s+(\d+)\s+\d+\.+\d+'}
with open('ch4_int_coord.txt') as f:
for index,line in enumerate(f,1):
print re.search(regex_dict[index],line)
