为什么我会得到语法:' yart'外部功能?

发布于 2025-02-08 09:22:22 字数 551 浏览 2 评论 0原文

我正在尝试使用Pycharm读取锁定的Excel文件,并且我使用了以下代码:

with open('abcd.xlsm', 'rb') as abcd1:
        excel = msoffcrypto.OfficeFile('abcd.xlsm')
        excel.load_key('1234')
        excel.decrypt(temp)
        abcd1.seek(0, os.SEEK_END)
        while True:
            lines = abcd1.readline()
            if not lines:
                time.sleep(0.1)
                continue
            yield lines

有人可以告诉我,此收益率功能有什么问题?我一直在得到

[syntaxerror:'farter'外部功能]

我是Python的新手,所以我感谢您的帮助,非常感谢!

I am trying to use PyCharm to read a locked excel file and I used the following code:

with open('abcd.xlsm', 'rb') as abcd1:
        excel = msoffcrypto.OfficeFile('abcd.xlsm')
        excel.load_key('1234')
        excel.decrypt(temp)
        abcd1.seek(0, os.SEEK_END)
        while True:
            lines = abcd1.readline()
            if not lines:
                time.sleep(0.1)
                continue
            yield lines

Can anyone tell me what's wrong with this yield function? I have been getting

[SyntaxError: 'yield' outside function]

I am new to python so I would appreciate some help, thank you so much!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

ぶ宁プ宁ぶ 2025-02-15 09:22:22

收益率是只能在函数中使用的关键字,您将其称为外部任何函数,因此无法正常工作

https://www.geeksforgeeks.org/python-yield-keyword/
您可以在这里或在官方文档中阅读更多有关它的信息
https:///docs.pypython.org/3/reference/ simple_stmts.html#YEILD-Statement

Yield is a keyword that can only be used in a function, you called it outside any function so that won't work

https://www.geeksforgeeks.org/python-yield-keyword/
You could read more about it here or in the official documentations
https://docs.python.org/3/reference/simple_stmts.html#the-yield-statement

隔岸观火 2025-02-15 09:22:22

错误“ SyntaxError:'屈服'外部功能”本身说“收益”关键字用于外部函数。

一个简单的示例如何使用产量如下:

DEF CONCOL_FACTOR(n):

for k in range(1,n+1):

    if n%k == 0:

        yield k

x = common_factors(100)

print(x)#prints< generator object object common_factors at 0x0000023efbbe1e70>

打印(列表(x))#打印[1、2、4、5、10、20、25、50、100]

The error "SyntaxError: 'yield' outside function" itself says the "yield" keyword is used outside function.

One simple example how to use yield is shown below:

def common_factors(n):

for k in range(1,n+1):

    if n%k == 0:

        yield k

x = common_factors(100)

print(x) # prints <generator object common_factors at 0x0000023EFBBE1E70>

print(list(x)) # prints [1, 2, 4, 5, 10, 20, 25, 50, 100]

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文