python : How to find block comments string in Python

标签 python string parsing python-2.7

我想在Python中解析 block 注释。我为了做到这一点,需要在 .py 文件中找到注释部分。

例如,我的s​​ample.py包含

'''
This is sample file for testing
This is sample file for development
'''
import os
from os import path
def somefun():
    return

我想解析我正在尝试在sampleparsing.py中执行以下操作的注释部分

tempfile = open('sample.py', 'r')
for line in tempfile: 
    if(line.find(''''') != -1):
        dosomeoperation()
        break

既然 if(line.find(''''') != -1): 假设下面的剩余行已注释,如何替换此字符串以查找注释?

我尝试在中间保留“\”(转义字符),但找不到此问题的解决方案。

在sampleparsing.py中解析后我需要以下两行:

This is sample file for testing
This is sample file for development

最佳答案

尝试使用 "'''" 作为您的字符串。现在Python认为你仍在写一个字符串。

关于 python : How to find block comments string in Python,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18063837/

相关文章:

python - python 中的 sha256(new Buffer(string, 'hex' ))

Python 文件类型

Python 套接字发送缓冲区与。强度

c++ - 字符串操作,转换时在字符串开头显示 0

java - 解析 Booking.uz.gov.ua 网站

python - 如何使用预训练的模型权重初始化新的 word2vec 模型?

python - wxPython:在现有 wx.Panel 上覆盖 wx.Panel 的好方法

python - 计算字符串中子字符串出现的次数

java - jackson JSON 库中的 ALLOW_UNQUOTED_FIELD_NAMES

ruby - 我如何使用 Nokogiri 查找网页上的特定文本/单词?