簡體   English   中英

按照特定模式提取字符串並存儲它們

[英]extract strings after a certain pattern and store them

我有一堆看起來像這樣的輸出:

 004400:  0x10000000 (268435456)
 004404:  0x0f010000 (251723776)
 004408:  0x0c018000 (201424896)
 00440c:  0x0c019000 (201428992)
 004410:  0x0b01a000 (184655872)
 004414:  0x0901a800 (151103488)
 004418:  0x0701aa00 (117549568)
 00441c:  0x0701aa80 (117549696)
 004420:  0x0701ab00 (117549824)
 004424:  0x0701ab80 (117549952)
 004428:  0x0701ac00 (117550080)
   .          .           .
   .          .           .
   .          .           .
 0047f4:  0x00000000 (0)
 0047f8:  0x00000000 (0)
 0047fc:  0x00000000 (0)

所以我想在地址(第一列)之后的第二列(例如: 0x10000000 )中提取內容。 以后我需要將它們寫回去,這樣可以更好地將它們存儲在文件中,然后以列表形式讀回。 我是Python的新手,並且想找到一個易於使用的庫。 一些例子將是很好的。 非常感謝。

從返回第二列的索引1獲取匹配的組。

(?<=:)\s*(0x.*?\b)

演示


也嘗試這個

[^:]\s*(0x.*?\b)

演示

描述:

(?<=:)     Positive Lookbehind to match the character : literally
[^:]       match a single character that is not :
\s*        match any white space character 
.*?        matches any character (except newline) lazily
\b         assert position at a word boundary

樣例代碼:

import re
p = re.compile(ur'[^:]\s*(0x.*?\b)')
test_str = ...

re.findall(p, test_str)

看看這個演示 ,以分組所有列。

如果您只想第二列的內容,則這里不需要正則表達式。 你可以做:

with open('myfile.txt', 'r') as f:
    col2 = [line.split()[1] for line in f]

這將為您提供第二列的內容,作為列表col2 如果要將這些內容寫入新文件,可以執行以下操作:

with open('outfile.txt', 'w') as f:
    for line in col2:
        f.write(line + '\n')

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM