简体   繁体   English

使用 python 删除 C 程序文件中的注释后获取新行

[英]Get a new line after removing comment in C program file with python

1st code第一个代码

1

2nd code第二个代码

2

copy = open("output.txt", "w")
with open('code.txt',"r") as f:
   for line in f:
     line = line.split('/',1 )[0]
     copy.write(line)
copy=open("output.txt","r")
print(copy.read())

here it removes a comment by deleting the separate line where the comment is deleted.在这里,它通过删除删除评论的单独行来删除评论。 I want to keep a new line space where the comment was deleted in the C program.我想在 C 程序中删除注释的位置保留一个新行空间。

import re

with open('code.txt', 'r') as f:
    lines = f.read()
    newlines = re.sub(r'/\*.*\*/', '\n', lines, flags=re.DOTALL)

with open('output.txt', 'w') as f:
    f.write(newlines)

re.DOTALL allows you to capture a pattern across lines re.DOTALL 允许您跨行捕获模式

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM