[英]read a file and search for a string, if matches, return the next word in python
我是python的新手,我正在嘗試執行以下操作:讀取給定的文件(“ file.txt”),如果匹配則搜索特定的字符串(“ string”),然后返回下一個單詞。 “ file.txt”如下:
...
func(string=0x41004578, val=0x01)
...
...
因此,如果文件中的“字符串”匹配,我希望得到“ 0x41004578”的值作為回報。
這個怎么做? 我完全空白。 我正在嘗試關注
file = open('file.txt', 'r').read().find('string')
通常用於這種事情的工具是grep。 因此,除了在Python中,讓我們做同樣的事情。
with open('file.text', 'r') as fp:
for line in fp:
match = re.search('string=([^,]+)', line)
if match:
print match.group(1)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.