Python Forum
Select Python comment (#) [solved]
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Select Python comment (#) [solved]
#1
Thumbs Up 
Hi,

I'm trying to select the Python comment (one line)
#comment example
I'm trying with the so call "Regular Expression" RegEx But so far I only managed to catch only the multi line """ or '''

Bur the one-liner # that seem easier , is not ! I've dig the WWW , but the majority of the example contains bugs !!

It's so weird as for example the coloration done by Vscodium can catch where is the comment (and the Python parser too)
[Image: GtAi7go.jpeg]


Any ideas or where to look ?
BTW other solution that RegEx are welcome too.

Thanks.
[Image: NfRQr9R.jpg]
Reply
#2
(Sep-12-2025, 11:24 AM)SpongeB0B Wrote: Hi,

I'm trying to select the Python comment (one line)
#comment example
I'm trying with the so call "Regular Expression" RegEx But so far I only managed to catch only the multi line """ or '''

Can you show what expression you've tried? Giving an example with what output you expect and what output you're getting instead would help.
Reply
#3
Something like this?

import regex

source_file = '/home/peterr/PVE/pandas/flatten_a_dictionary.py'

with open(source_file) as infile:
    text = infile.readlines()

# no need for regex but won't find indented comments
comments = [t for t in text if t[0] == '#']

with open(source_file) as infile:
    s = infile.read()

# .* finds everything in a line except the end of line
# maybe there is some space before the #
# ^\n = not newline, otherwise we pick up the newlines as space before #
f = regex.compile(r'[^\n\s+]?#.*')
res = regex.findall(f, s )
f finds these comments:

Output:
['# make df from lists', '# make df from dictionary', '# problem is info is a list of dictionaries', '# to flatten this data, we need to get info and open it up', '# not nice', '# same as pd.DataFrame(data)', '# make a new dict with lists of ages, genders and locations as well as id and names', '# cunning indented comment here', '# cunning indented comment here', '# successfully flattened ']
You could probably modify:

comments = [t for t in text if t[0] == '#']
to find indented comments without using a regex. Try it!
Reply
#4
Maybe the ast (abstract system tree) is a better choice for finding comments in python code.
Reply
#5
Thank you all for you inputs.

I could managed it with tokenize

Work well, but oh god ! the documentation is not at all complete !
[Image: NfRQr9R.jpg]
Reply
#6
Something like this using tokenize and regex.
import io
import re
import tokenize

def find_python_comments(source: str, CODING_RE: re.Pattern[str]):
    comments = []
    reader = io.StringIO(source).readline
    for tok in tokenize.generate_tokens(reader):
        if tok.type == tokenize.COMMENT:
            line_no = tok.start[0]
            text = tok.string
            # Skip shebang and coding cookie if you don't want them
            if line_no == 1 and text.startswith("#!"):
                continue
            if line_no <= 2 and CODING_RE.fullmatch(text.strip()):
                continue
            comments.append((line_no, text))   # (line number, comment text)
    return comments

# Example
code = """\
#! /usr/bin/env python3
x = 1  # inline comment
s = '# not a comment'
\"\"\"a triple-quoted string with # inside\"\"\"
'''# Hello World '''
# real comment
python.code('# This is not comment') # A comment #Part of comment
"""

if __name__ == '__main__':
    CODING_RE = re.compile(r"#.*coding[:=]\s*[-\w.]+", re.I)
    print(find_python_comments(code, CODING_RE))
Output:
[(2, '# inline comment'), (6, '# real comment'), (7, '# A comment #Part of comment')]
Reply
#7
Amazing what one can think up a 4am with the wind howling outside!

source_file = '/home/peterr/PVE/pandas/flatten_a_dictionary.py'
 
with open(source_file) as infile:
    text = infile.readlines()

comments = [t.split('#')[1] for t in text if '#' in t]
If there are more than 1 # in a line:

# if there are more than 1 # in a line then need some more magic, but works with only 1 # per line too
comments = ['_|_'.join(t.split('#')[1:]) for t in text if '#' in t]
tester_V likes this post
Reply
#8
to Pedroski55: I like how you think! You are something...
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Is there a Python solution to select unique frames from a video? rownong 1 1,399 Feb-02-2025, 09:56 AM
Last Post: Larz60+
  How to add multi-line comment section? Winfried 2 2,017 Jun-04-2024, 07:24 AM
Last Post: Gribouillis
Question remove all comment ? SpongeB0B 7 16,920 Oct-27-2023, 05:40 PM
Last Post: deanhystad
  Regular Expression search to comment lines of code Gman2233 5 3,857 Sep-08-2022, 06:57 AM
Last Post: ndc85430
  [SOLVED] [BeautifulSoup] Turn select() into comma-separated string? Winfried 0 2,434 Aug-19-2022, 08:07 PM
Last Post: Winfried
  Error using mariadb select query with form in python? shams 2 3,477 Jul-29-2021, 12:30 PM
Last Post: shams
  Block comment in Intellij kam_uk 2 4,293 Nov-15-2020, 05:22 PM
Last Post: kam_uk
  Python 3 Elementtree and Comment First gw1500se 3 6,478 May-25-2020, 09:02 PM
Last Post: gw1500se
  Youtube Comment bot mateusz135 4 103,723 Mar-14-2020, 04:18 PM
Last Post: buran
  Python and Postgresql syntax select statement Nesreenmhd 1 6,357 Sep-07-2019, 06:08 PM
Last Post: ndc85430

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020