简体   繁体   中英

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? 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:

    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.

Thank you!

Don't just open a file - it needs to be closed, too. Best to use a context manager 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.

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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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