如何为文件中的每一行使用不同形式的正则表达式?

时间:2022-01-31 06:12:53
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

this is what I have so far. I'm trying to use a new regex pattern for each line in the file (since each has a different amount of data)but I'm not sure how to specify this.

这就是我到目前为止所拥有的。我正在尝试为文件中的每一行使用一个新的正则表达式模式(因为每个都有不同的数据量),但我不知道如何指定它。

1 个解决方案

#1


2  

You can use a dictionary to preserve your regexes and access them with a simple indexing, can put the relative number of lines as the key and iterate over the file object using enumerate function in order to access to the line index.

您可以使用字典来保留正则表达式并使用简单的索引访问它们,可以将相对行数作为键,并使用枚举函数迭代文件对象以访问行索引。

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)

#1


2  

You can use a dictionary to preserve your regexes and access them with a simple indexing, can put the relative number of lines as the key and iterate over the file object using enumerate function in order to access to the line index.

您可以使用字典来保留正则表达式并使用简单的索引访问它们,可以将相对行数作为键,并使用枚举函数迭代文件对象以访问行索引。

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)