今天在看pygments的代码的时候,发现有一个类是 自生成式的 就是在main方法里面 通过一系列操作写文件
当然 这个文件就是他自己了
其中的代码片段
"""
pygments.lexers._mapping
~~~~~~~~~~~~~~~~~~~~~~~~
Lexer mapping defintions. This file is generated by itself. Everytime
you change something on a builtin lexer defintion, run this script from
the lexers folder to update it.
Do not alter the LEXERS dictionary by hand.
:copyright: Copyright 2006-2010 by the Pygments team, see AUTHORS.
:license: BSD, see LICENSE for details.
"""
LEXERS = {
'ABAPLexer': ('pygments.lexers.other', 'ABAP', ('abap',), ('*.abap',), ('text/x-abap',)),
}
if __name__ == '__main__':
import sys
import os
# lookup lexers
found_lexers = []
sys.path.insert(0, os.path.join(os.path.dirname(__file__), '..', '..'))
for filename in os.listdir('.'):
if filename.endswith('.py') and not filename.startswith('_'):
module_name = 'pygments.lexers.%s' % filename[:-3]
print module_name
module = __import__(module_name, None, None, [''])
for lexer_name in module.__all__:
lexer = getattr(module, lexer_name)
found_lexers.append(
'%r: %r' % (lexer_name,
(module_name,
lexer.name,
tuple(lexer.aliases),
tuple(lexer.filenames),
tuple(lexer.mimetypes))))
# sort them, that should make the diff files for svn smaller
found_lexers.sort()
# extract useful sourcecode from this file
f = open(__file__)
try:
content = f.read()
finally:
f.close()
header = content[:content.find('LEXERS = {')]
footer = content[content.find("if __name__ == '__main__':"):]
# write new file
f = open(__file__, 'w')
f.write(header)
f.write('LEXERS = {\n %s\n}\n\n' % ',\n '.join(found_lexers))
f.write(footer)
f.close()
前面的是生成一个列表
关键是地方在
# extract useful sourcecode from this file
f = open(__file__)
try:
content = f.read()
finally:
f.close()
header = content[:content.find('LEXERS = {')]
footer = content[content.find("if __name__ == '__main__':"):]
# write new file
f = open(__file__, 'w')
f.write(header)
f.write('LEXERS = {\n %s\n}\n\n' % ',\n '.join(found_lexers))
f.write(footer)
f.close()
就是打开文件,然后依次写头部。写尾部。首先把文件读取出来。字符才转换成列表的形式
然后用find()的方法 找到要查找的串的第一个元素开始的位置。然后就是用列表的切片来截取了
有一个地方用了%r 格式化
经过我的测试 %r 和%s相比 %r会把回车换行这些转换成\n
这样写文件的话 就不会因为系统的差异而导致文件格式不一致 甚至出现问题了
分享到:
相关推荐
主要介绍了C语言字符串转换为Python字符串的方法,文中讲解非常细致,代码帮助大家更好的理解和学习,感兴趣的朋友可以了解下
本文实例为大家分享了python实现字符串加密成纯数字的具体代码,供大家参考,具体内容如下 说明: 该加密算法仅仅是做一个简单的加密,安全性就不谈了,哈哈. 算法流程:
python实现字符串模糊匹配
python字符串学习笔记.python字符串操作方法
python字符串, 列表, 字典, 集合方法说明
Python字符串操作常用函数,包含了检索、统计、分割、替换、大小写转换、对齐,空格删除、字符串判断(头尾+组成)
python串口发送数据与接收数据(文本转hex 接收字符串或者列表类型)
python字符串操作.pdf
Python 清洗字符串和列表 Python源码Python 清洗字符串和列表 Python源码Python 清洗字符串和列表 Python源码Python 清洗字符串和列表 Python源码Python 清洗字符串和列表 Python源码Python 清洗字符串和列表 Python...
python字符串学习笔记
大熊课堂,关于 python 字符串的课件
Python 如何实现字符串与列表等数据的去重 Python源码Python 如何实现字符串与列表等数据的去重 Python源码Python 如何实现字符串与列表等数据的去重 Python源码Python 如何实现字符串与列表等数据的去重 Python源码...
python字符串操作实方法大合集,包括了几乎所有常用的python字符串操作,如字符串的替换、删除、截取、复制、连接、比较、查找、分割等,需要的朋友可以参考下。
python字符串处理实例.docx
1、字符串的遍历,和列表类似,可以把字符串当做元素都是一个字符的一个字符列表,它可以和列表有公共的语法 2、分不同的类别,若有符合条件的字符直接在类别数上加1就可以 初始代码: # -*- coding:utf-8 -*- ...
Python 字符串深度总结Python 字符串深度总结
Python_字符串处理.txt
Python 拼接字符串、列表和字典 Python源码Python 拼接字符串、列表和字典 Python源码Python 拼接字符串、列表和字典 Python源码Python 拼接字符串、列表和字典 Python源码Python 拼接字符串、列表和字典 Python源码...
本文实例讲述了python转换字符串为摩尔斯电码的方法。分享给大家供大家参考。具体实现方法如下: chars = ",.0123456789?abcdefghijklmnopqrstuvwxyz" codes = """--..-- .-.-.- ----- .---- ..--- ...-- ....- ......