gpt4 book ai didi

python : How to find block comments string in Python

转载 作者:行者123 更新时间:2023-11-30 23:34:30 25 4
gpt4 key购买 nike

我想在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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com