python - 如何删除 C 文件中的多行注释以使用 Python?
问题描述
我正在使用 .readlines() 读取 C 文件,以获取 C 代码文件的所有单独行作为列表的元素。如果任何列表元素(即 C 代码行)是多行注释的一部分,则需要将其替换为 ''(即删除它但它将占用列表的相应索引),以便不更改其他行的索引. 请检查我的代码以获取详细信息。
import re
#open and read sample.c so that code lines of sample.c are the element of list 'flist'
with open('sample.c',mode='r') as myfile:
flist = myfile.readlines()
pattern1 = re.compile(r'\/{2}.*') # to match //
pattern2 = re.compile('\/\*.*\*\/') # to match /*......*/ in a single line or element of flist
pattern3 = re.compile('\/\*') # to match /*
pattern4 = re.compile('\*\/') # to match */
# to delete comments present in a single code line or can say in an individual element of 'flist', it's working fine.
for index,line in enumerate(flist):
if pattern2.search(line):
flist[index] = pattern2.sub("",line)
if pattern1.search(line):
flist[index] = pattern1.sub("",line)
# to delete comment starting in one line and ending in the subsequent line, **this following part is not working**
cmnt_e = True
cmnt_s = False
for index,line in enumerate(flist):
if pattern3.search(line) and cmnt_e == True:
cmnt_s = True
cmnt_e = False
if pattern3.search(line) and cmnt_s == True:
cmnt_e = True
cmnt_s = False
if cmnt_s == True and cmnt_e == False:
flist[index] = ''
解决方案
我并没有看太多细节,但是您正在使用 pattern3 来开始和结束评论。我想它应该是这样的
if pattern3.search(line) and cmnt_e == True:
cmnt_s = True
cmnt_e = False
if pattern4.search(line) and cmnt_s == True:
cmnt_e = True
cmnt_s = False
我也会警惕像这样的奇怪模式(是的,我有时将它用于实验性的东西,你只需翻转第一个斜线来翻转活动代码):
//*
ACTIVE_CODE
/*/
INACTIVE_CODE
//*/
推荐阅读
- sql - 如何在 Jupyter Notebook 中使用 Pyodbc 从 SQL Server 存储过程中检索多行
- ruby-on-rails - 罗尔。计算轨道中对象中“否”、“是”和“不适用”的数量
- networking - 使用 DNS 名称的 Pod 到 Pod 通信
- javascript - OPTIONS 请求不一致(在部署中)?
- python-3.x - 为什么我的 django unittest 未能通过约束?
- angular - Angular:如何用 routerLink 覆盖嵌套的 href?
- docker - 使用 Docker 构建 PLC4X 时如何解决此错误
- python - 如何修复 SettingWithCopyWarning
- python - NameError:名称'pip'未定义
- swift - 如果从 Core Data SwiftUI 中删除,则删除本地通知