Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save iHTCboy/2dc975206667af92bb6f0a364ab11e45 to your computer and use it in GitHub Desktop.
Save iHTCboy/2dc975206667af92bb6f0a364ab11e45 to your computer and use it in GitHub Desktop.
Python读取文件和保存内容到文件
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