勤学教育网合作机构> > 郑州兄弟连教育欢迎您!

python注释符号

郑州兄弟连教育logo
来源:郑州兄弟连教育

2022-04-15|已帮助:7230

进入 >

python中的注释有多种,有单行注释,多行注释,批量注释,中文注释也是常用的。下面是小编为您整理的关于python注释符号,希望对你有所帮助。


python注释符号配图

python注释符号

python中的注释有多种,有单行注释,多行注释,批量注释,中文注释也是常用的。python注释也有自己的规范,在文章中会介绍到。注释可以起到一个备注的作用,团队合作的时候,个人编写的代码经常会被多人调用,为了让别人能更容易理解代码的通途,使用注释是非常有效的。

一、python单行注释符号(#)

井号(#)常被用作单行注释符号,在代码中使用#时,它右边的任何数据都会被忽略,当做是注释。

print 1 #输出1

#号右边的内容在执行的时候是不会被输出的。

二、批量、多行注释符号

在python中也会有注释有很多行的时候,这种情况下就需要批量多行注释符了。多行注释是用三引号''' '''包含的

python正则表达式的注释方法

学过正则都知道,那简直是天书,为了提高正则的可读性,正则表达式中提供了X(VERBOSE): 详细模式。这个模式下正则表达式可以是多行,忽略空白字符,并可以加入注释。

例如:

import re

str = 'python regex'

pattern = re.compile(r'''

(w+) # first word

s(w+) # second word

''', re.X)

match = re.match(pattern,str)

if match:

print "%s %s"%(match.group(2),match.group(1))

其实,由于在python语法里,小括号里面的字符串是可以分行写,所以我们也可以不用X模式来写正则表达式的注释:

import re

str = 'python regex'

pattern = re.compile(r'(w+)' #first word

r' (w+)' #second word

)

match = re.match(pattern,str)

if match:

print "%s %s"%(match.group(2),match.group(1))

大家可以根据自己的爱好来给自己的正则注释起来。

用Python将注释行和空行去掉

比如要将/etc/httpd/conf/httpd.conf的注释行和空行去掉并且打印,用一行命令就可以做到:


python注释符号配图

egrep -v ‘^#|^$’ /etc/httpd/conf/httpd.conf。但这里练习用Python实现

#!/usr/bin/env python

#coding: utf8

import os

def dellines():

#os模块调用linux命令,cp是为了避免alias里面的cp -i,强制复制文件,不询问是否覆盖

os.system('cp -r -f /etc/httpd/conf/httpd.conf .')

f = file('httpd.conf')

linenum = 0

while True:

data = f.readline()

if data == '':

break

else:

#第一个字符为#或者是换行符,就pass,否则就打印这一行

if (data[0] == '#') or (data[0] == ' '):

pass

else:

linenum += 1

print linenum, data ,

f.close()

if __name__ == '__main__':

dellines()

Python去掉文件中空行

# coding = utf-8

def clearBlankLine():

file1 = open('text1.txt', 'r', encoding='utf-8') # 要去掉空行的文件

file2 = open('text2.txt', 'w', encoding='utf-8') # 生成没有空行的文件

try:

for line in file1.readlines():

if line == ' ':

line = line.strip(" ")

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')

_map = { }

outstring = ''

line = fdr.readline()

while line:

while True:

#这里要注意,我用的是re.S 比如print("aaa ")

m = re.compile('".*"', re.S)

_str = m.search( line )

#如果没匹配成功,就合并,然后下一行

if None == _str:

outstring += line

break

key = str( uuid.uuid1() )

#

m = re.compile('".*"', re.S)

outtmp = re.sub(m, key, line, 1)

line = outtmp

_map[ key ] = _str.group(0)

line = fdr.readline()

m = re.compile(r'//.*')

outtmp = re.sub(m, ' ', outstring)

outstring = outtmp

m = re.compile(r'/*.*?*/', re.S)

outtmp = re.sub(m, ' ', outstring)

outstring = outtmp

for key in _map.keys():

outstring = outstring.replace(key, _map[key])

fdw.write(outstring)

fdw.close()

以上是郑州兄弟连教育整理的python注释符号全部内容。

热门推荐

更多
勤学培训网 python学习网 python注释符号