ファイルを入力として受け付け、フルラインのコメントである行数を出力する関数を作成しようとしています(つまり、コメントは#で始まります)。Pythonでフルラインコメントの行数をどのように数えますか?
は、たとえば次の行は、結果2を印刷する必要があります言う含むファイル:
abc
#some random comment
cde
fgh
#another random comment
これまでのところ、私はの線に沿ってみましたが、ちょうどハッシュ記号拾っていない:
infile = open("code.py", "r")
line = infile.readline()
def countHashedLines(filename) :
while line != "" :
hashes = '#'
value = line
print(value) #here you will get all
#if(value == hashes): tried this but just wasn't working
# print("hi")
for line in value:
line = line.split('#', 1)[1]
line = line.rstrip()
print(value)
line = infile.readline()
return()
を
ありがとうございます。 Jemma