0
我試圖編寫一個程序,該程序具有通過在文檔字符串中查找Author
字符串來查找和打印文件作者的功能。我已經設法得到下面的代碼來打印一個文件的作者,該文件的作者字符串後面跟着作者姓名,作者字符串後面跟着一個名字。我遇到的問題是當作者字符串根本不存在時試圖打印Unknown
,即文檔字符串的任何部分都不包含Author
。從docstring查找python文件的作者
N.B. lines
只是在文件上使用readlines()
構建的列表。
def author_name(lines):
'''Finds the authors name within the docstring'''
for line in lines:
if line.startswith("Author"):
line = line.strip('\n')
line = line.strip('\'')
author_line = line.split(': ')
if len(author_line[1]) >=4:
print("{0:21}{1}".format("Author", author_line[1]))
else:
print("{0:21}{1}".format("Author", "Unknown"))
感謝,這正是我所需要的幫助 – jevans