Created
March 18, 2020 08:42
-
-
Save iHTCboy/2dc975206667af92bb6f0a364ab11e45 to your computer and use it in GitHub Desktop.
Python读取文件和保存内容到文件
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import re | |
dump_file_path = '/Users/iHTCboy/Downloads/file.txt' | |
# 读取 dump 文件内容 | |
with open(dump_file_path, mode='r', encoding="utf8", errors="ignore" ) as flip: | |
dump = flip.read() | |
# 正则匹配 | |
pattern = r'@interface (.+) :' | |
r = re.compile(pattern) | |
rs = r.findall(dump) | |
dump_out_file_path = '/Users/iHTCboy/Downloads/save.txt' | |
for item in sorted(rs): | |
print(item) | |
if item: | |
# 'a' 追加内容, 'w' 为覆盖写入 | |
with open(dump_out_file_path, 'a') as flip: | |
flip.write(item + '\n') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment