python的注释符号是什么_python注释符号
python中的注释有多种,有单⾏注释,多⾏注释,批量注释,中⽂注释也是常⽤的。下⾯是⼩编为您整理的关于python注释符号,希望对你有所帮助。
python注释符号
python中的注释有多种,有单⾏注释,多⾏注释,批量注释,中⽂注释也是常⽤的。python注释也有⾃⼰的规范,在⽂章中会介绍到。注释可以起到⼀个备注的作⽤,团队合作的时候,个⼈编写的代码经常会被多⼈调⽤,为了让别⼈能更容易理解代码的通途,使⽤注释是⾮常有效的。
python基础代码注释
⼀、python单⾏注释符号(#)
井号(#)常被⽤作单⾏注释符号,在代码中使⽤#时,它右边的任何数据都会被忽略,当做是注释。
print 1 #输出1
#号右边的内容在执⾏的时候是不会被输出的。
⼆、批量、多⾏注释符号
在python中也会有注释有很多⾏的时候,这种情况下就需要批量多⾏注释符了。多⾏注释是⽤三引号''' '''包含的
python正则表达式的注释⽅法
学过正则都知道,那简直是天书,为了提⾼正则的可读性,正则表达式中提供了X(VERBOSE): 详细模式。这个模式下正则表达式可以是多⾏,忽略空⽩字符,并可以加⼊注释。
例如:
import re
str = 'python regex'
pattern = repile(r'''
(w+) # first word
s(w+) # second word
''', re.X)
match = re.match(pattern,str)
if match:
print "%s %s"%(up(2),up(1))
其实,由于在python语法⾥,⼩括号⾥⾯的字符串是可以分⾏写,所以我们也可以不⽤X模式来写正则表达式的注释:
import re
str = 'python regex'
pattern = repile(r'(w+)' #first word
r' (w+)' #second word
)
match = re.match(pattern,str)
if match:
print "%s %s"%(up(2),up(1))
⼤家可以根据⾃⼰的爱好来给⾃⼰的正则注释起来。
⽤Python将注释⾏和空⾏去掉
⽐如要将/etc/httpd/f的注释⾏和空⾏去掉并且打印,⽤⼀⾏命令就可以做到:egrep -v ‘^#|^$’ /etc/httpd/f。但这⾥练习⽤Python实现
#!/usr/bin/env python
#coding: utf8
import os
def dellines():
#os模块调⽤linux命令,cp是为了避免alias⾥⾯的cp -i,强制复制⽂件,不询问是否覆盖
os.system('cp -r -f /etc/httpd/f .')常见视图模式作用
f = file('f')
linenum = 0
while True:
data = f.readline()
if data == '':
break
else:
#第⼀个字符为#或者是换⾏符,就pass,否则就打印这⼀⾏
if (data[0] == '#') or (data[0] == 'n'):
pass
else:
linenum += 1
print linenum, data ,
f.close()
if __name__ == '__main__':
dellines()
Python去掉⽂件中空⾏
# coding = utf-8
def clearBlankLine():
file1 = open('', 'r', encoding='utf-8') # 要去掉空⾏的⽂件
file2 = open('', 'w', encoding='utf-8') # ⽣成没有空⾏的⽂件
try:
for line adlines():
if line == 'n':
line = line.strip("n")
file2.write(line)
finally:
file1.close()
file2.close()
if __name__ == '__main__':
clearBlankLine()
利⽤PYTHON的正则表达式去掉代码中的注释
校招时,百度⼆⾯的时候,让我写⼀个删除代码中的注释的代码,当时卡壳了。时隔⼀年多,想起这个问题,现在把这个写下来。
先说⼀下代码的思想,⾸先将“字符串”进⾏替换,替换成 uuid ,并且把字符串的内容存起来。_map是作为字典,uuid作为key,字符串内容作为value。
然后再把// 和 /**/ 进⾏替换
最后输出到⽂件中
import re
import uuid
fdr = open("input.c", 'r')
fdw = open("output.c", 'w')
postman中文翻译_map = { }
outstring = ''
line = adline()
while line:
while True:
#这⾥要注意,我⽤的是re.S ⽐如print("aaan")
m = repile('".*"', re.S)
_str = m.search( line )
#如果没匹配成功,就合并,然后下⼀⾏
if None == _str:
outstring += line
break
key = str( uuid.uuid1() )
#
m = repile('".*"', re.S)
outtmp = re.sub(m, key, line, 1)
line = outtmpmac里的excel数组运算怎么按
_map[ key ] = _up(0)补码运算是否溢出
line = adline()
m = repile(r'//.*')
outtmp = re.sub(m, ' ', outstring)
正则限制特殊符号的输入
outstring = outtmp
m = repile(r'/*.*?*/', re.S)
outtmp = re.sub(m, ' ', outstring)
outstring = outtmp
for key in _map.keys():
outstring = place(key, _map[key]) fdw.write(outstring)
fdw.close()