python的注释符号有哪些_python注释符号

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

6c796672e59926cf1cdfec2556c4c109.jpg

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的注释行和空行去掉并且打印,用一行命令就可以做到:

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] == 'n'):

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

_map = { }

outstring = ''

line = fdr.readline()

while line:

while True:

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

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

  • 1
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值