使用python文本处理模块编辑多个文件
massedit的Python项目详细描述
massedit
以前称为python mass editor
实现一个python mass编辑器来使用python处理文本文件 代码。修改作为diff输出显示在stdout上。一 然后可以使用-w/-write选项修改目标文件。 这与python 3附带的2to3工具非常相似。
<表> < COLGROUP > < COL/> <正文> 警告 :有关使用 eval() 的警告这个工具非常有用,但它确实依赖于python eval() 函数,不检查正在执行的代码。 这是一个重大的安全风险,不应在 生产环境。
有关危险的详细讨论,请参见Ned Batchelder的文章 链接到eval()和规避它们的方法。请注意 本文中建议的反措施此时实施。
<表>用法
您可能需要了解python re模块的基本知识 表达式)。
usage: massedit.py [-h] [-V] [-w] [-v] [-e EXPRESSIONS] [-f FUNCTIONS] [-x EXECUTABLES] [-s START_DIRS] [-m MAX_DEPTH] [-o output] pattern [pattern ...] Python mass editor positional arguments: pattern shell-like file name patterns to process. optional arguments: -h, --help show this help message and exit -V, --version show program's version number and exit -w, --write modify target file(s) in place. Shows diff otherwise. -v, --verbose increases log verbosity (can be specified multiple times) -e EXPRESSIONS, --expression EXPRESSIONS Python expressions applied to target files. Use the line variable to reference the current line. -f FUNCTIONS, --function FUNCTIONS Python function to apply to target file. Takes file content as input and yield lines. Specify function as [module]:?<function name>. -x EXECUTABLES, --executable EXECUTABLES Python executable to apply to target file. -s START_DIRS, --start START_DIRS Directory(ies) from which to look for targets. -m MAX_DEPTH, --max-depth-level MAX_DEPTH Maximum depth when walking subdirectories. -o FILE, --output FILE redirect output to a file -g FILE, --generate FILE generate input file suitable for -f option --encoding ENCODING Encoding of input and output files --newline NEWLINE Newline charachter for output files Examples: # Simple string substitution (-e). Will show a diff. No changes applied. massedit.py -e "re.sub('failIf', 'assertFalse', line)" *.py # File level modifications (-f). Overwrites the files in place (-w). massedit.py -w -f fixer:main *.py # Will change all test*.py in subdirectories of tests. massedit.py -e "re.sub('failIf', 'assertFalse', line)" -s tests test*.py
如果massedit是作为包安装的(例如来自pypi),则可以作为命令行工具与之交互:
python -m massedit -e "re.sub('assertEquals', 'assertEqual', line)" test.py
或作为库(上面的命令行选项将作为kewyord参数传递):
>>> import massedit >>> filenames = ['massedit.py'] >>> massedit.edit_files(filenames, ["re.sub('Jerome', 'J.', line)"])
最后,在 发行版。
安装
从http://github.com/elmotec/massedit下载massedit.py
pip install massedit
人-源-源操作不良
我发现自己主要使用massedit进行源到源的修改 像这样的大型代码库:
首先使用将 处理源代码。例如,要添加标题:
def add_header(lines, file_name): yield '// This is my header' # will be the first line of the file. for line in lines: yield line
将fixer.py的位置添加到您的 $pythonpath ,然后简单地 像这样调用massedit.py
massedit.py -f fixer:add_header *.h
您可以添加 -s 选项,以便以递归方式处理所有 .h 文件。
计划
- 添加对第三方工具的支持(例如 autopep8 )以处理文件。
- 添加对表达式文件的支持作为参数以允许多个 立即修改。
- 找到一个令人满意的方法(即易于使用)来处理多行regex 当前版本按行工作。
基本原理
- 我很难练习几种以上的方言 表达式。
- 我需要一些可以移植到windows的东西,而不必为eol而烦恼。
- 我相信python是构建比 简单的基于正则表达式的替换。
背景
我已经使用runsed和checksed(来自unix电动工具)很多年了,而且 直到我碰到格雷厄姆,才在窗户下找到一个好的替代品 在activestate上,fawcett python recipe 437932 。它激励我写 海量编辑。
核心有点肉质化了,我们到了。如果你觉得有用 请增强它,不要忘记提交补丁。谢谢!
如果您对类似awk的工具更感兴趣,您可能会发现 pyp a 更好的选择。这当然是一个更成熟的工具。
许可证
根据麻省理工学院许可证的条款获得许可。请参阅附件license.txt。
更改
< DL>参与者确认
Steven Myint, https://github.com/myint tgoodlet,https://github/tgoodlet" rel="nofollow">https://github/tgoodlet