简体   繁体   English

使用@classmethod 装饰器在 python 中运行代码时遇到问题

[英]Facing issue while running a code in python by using @classmethod decorator

I am trying to run the below command in my jupyter notebook, it's working fine cuz no error showed up but also no output. But if am trying the same command in an IDE it's working fine.我正在尝试在我的 jupyter 笔记本中运行以下命令,它工作正常,因为没有出现错误,但也没有 output。但是如果我在 IDE 中尝试相同的命令,它工作正常。 What can be the problem??可能是什么问题?

import csv

class Item:
    @classmethod 
    def instantiate_from_csv(cls): 
        with open('item.csv', 'r') as f:
            reader = csv.DictReader(f) #this method will read the content from csv file as a dictionary
            items = list(reader)

        for item in items:
            print(item)


Item.instantiate_from_csv()

I tried in jupyter, I didn't get the expected output, a list of all the items mentioned in the csv in the form of dictionary so then I tried in IDLE it worked fine and I got the expected output.我在 jupyter 中尝试过,我没有得到预期的 output,这是 csv 中提到的所有项目的字典形式的列表,所以我在 IDLE 中尝试它工作正常,我得到了预期的 output。

When you execute当你执行

        with open('item.csv') as f:

the CWD, current working directory, is pretty important. CWD,当前工作目录,非常重要。

Use os.getcwd() to verify it is what you expect.使用os.getcwd()来验证它是否符合您的预期。

If it isn't, consider switching from a relative to an absolute pathname, that looks like /some/where/item.csv .如果不是,请考虑从相对路径名切换为绝对路径名,看起来像/some/where/item.csv


Your previous efforts might have accidentally created a zero-length data file.您之前的努力可能不小心创建了一个零长度的数据文件。 If you notice such a.CSV file, just delete it, so that future attempts to read it will produce a more informative diagnostic error message.如果您注意到这样一个 .CSV 文件,只需将其删除,以便将来尝试读取它时会产生信息更丰富的诊断错误消息。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM