简体   繁体   中英

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. 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.

When you execute

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

the CWD, current working directory, is pretty important.

Use os.getcwd() to verify it is what you expect.

If it isn't, consider switching from a relative to an absolute pathname, that looks like /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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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