Py学习  »  Python

如何在python中搜索一行中的字符串并提取两个字符之间的数据

Rekha G • 5 年前 • 1712 次点击  

文件内容:

module traffic(
    green_main, yellow_main, red_main, green_first, yellow_first, 
    red_first, clk, rst, waiting_main, waiting_first
);

我需要搜索字符串'module',我需要提取(……);括号之间的内容。

这是我试过的代码,我无法得到结果

fp = open(file_name)
contents = fp.read()
unique_word_a = '('
unique_word_b = ');'
s = contents

for line in contents:
    if 'module' in line:
        your_string=s[s.find(unique_word_a)+len(unique_word_a):s.find(unique_word_b)].strip()
        print(your_string)
Python社区是高质量的Python/Django开发社区
本文地址:http://www.python88.com/topic/44329
 
1712 次点击  
文章 [ 2 ]  |  最新文章 5 年前