python - SyntaxError: 'return' 在注释中生成的外部函数
问题描述
myfile=open("output_log.text", "r")
for file in myfile:
count = 0
for word in file:
if word == "Jenkins":
count = count + 1
return word
print(int(word))
上面的编码产生了我在上面的标题中提到的语法错误。有谁可以帮我解决这个问题?感谢大家。
解决方案
OK, you have not defined a function yet. :-)
####### Beginning of python script #######
def myfunc(myword, myfile):
# rest of your function code.
for file in myfile:
count = 0
for word in file:
if word == myword:
count = count + 1
return myword, count
# Now call the function from outside.
# Set myfile and myword variables.
myfile = open(r'c:\python\so\output_log.text', "r")
myword = 'Jenkins'
results = myfunc(myword, myfile)
print(results)
# Example print output.
> ('Jenkins', 4)
# You are passing myfile and myword as variables in your function call.
# You can changes these later for a different file and a different word.
推荐阅读
- laravel - 我如何获得满足在某个城市的标准的所有单位(其中城市是属性表中的一列)
- kubernetes - 使用 Prometheus 抓取 Kubernetes 元数据标签
- mongodb - MongoDB查询仅显示大于平均值的数据
- html - 通过 Netlify 使用默认的 Hugo 主题内容会产生空白网页,但可以在本地工作。博客下载
- macos - 如何在 mac 上清理 VSCode、Code 目录?
- loops - vbscript 检查是否存在多个进程并杀死它们
- git - 如何回复或评论 Linux 内核邮件列表中的线程?
- php - PHP Websocket 客户端
- r - 如何用ggplot在直方图上绘制密度?
- sql - 与 SQL 中的表不同的值行