简体   繁体   English

阅读行时忽略注释(特定字符后的字符)

[英]Ignoring comments (characters after a certain character) when reading lines

does any of you know a good scheme to get rid of comments when reading in a file line by line in Py3?你们中有人知道在 Py3 中逐行读取文件时去除注释的好方案吗? I don't want to use regex if possible at all.如果可能的话,我根本不想使用正则表达式。

Assume the content of the file looks similar to this:假设文件的内容类似于:

#first comment
while prime_count < n:#second comment
        for number in range(2, current):
            if current % number==0:#third comment
                break

I usually read the content via:我通常通过以下方式阅读内容:

file = open(refname, "r")
lines = file.readlines()
print(lines)

The output should be: output 应该是:

    while prime_count < n:
            for number in range(2, current):
                if current % number==0:
                    break

Any hint towards a certain direction would be helpful.任何指向某个方向的提示都会有所帮助。 The roughly 500 files will be in the order of 5000 characters.大约 500 个文件将按 5000 个字符的顺序排列。

Thank you!谢谢!

Don't just open a file - it needs to be closed, too.不要只open一个文件——它也需要关闭。 Best to use a context manager with :最好使用上下文管理with

with open(refname, "r") as file:
    # only keep those lines that do not start with a pound sign (after removing any whitespace)
    lines = [l for l in file.readlines() if not l.lstrip().startswith("#")]
print(lines)

Thanks to bram-vanroy to guiding me to strip/split - haven't thought of that before.感谢 bram-vanroy 指导我剥离/拆分 - 以前没有想到过。

with open(refname, "r") as file:
    for line in file:
        line = line.split('#', 1)[0]
        print(line)

will do what I want.会做我想做的。 Splitting the line at the pound sign and only keeping the first part.在井号处拆分线,只保留第一部分。

Another version, that gets rid of multiple blank lines, but uses regex:另一个版本,去掉了多个空行,但使用正则表达式:

with open(refname) as file:
    for line in file:
        line = line.split('#', 1)[0]+"\n"
        line = re.sub(r'\n+', '\n',line)
        if line.strip():
            list.append(line)

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

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