0

When I access csv.reader from a script, every thing works well but when I access it from a method inside my class, I get the following error:

AttributeError: 'GetInstgramUsernames' object has no attribute 'reader'

I have looked at these two similar issues my problem is different

Based on those two links, I have ensured that

  • A. I am pointing to the correct CSV libray
  • B. I don't have a csv.py file in my project

I am new to Python so it could just be a simple oversight, but I have included the code and comments on what works and what doesn't

import csv

print(csv.__file__)
# displays: 
# D:\ProgramData\Anaconda2\lib\csv.pyc

reader = csv.reader(open('D:\\dev\\scrapy\\instagram_influencers\\instagram_influencers\\input\\user_names.csv','r'))

print(list(reader))
# displays
# [['user_name'], ['mensfashionpost'], ['creativefasion']]

class GetInstgramUsernames(object):

    def read(self):
        # same line as above but produces an error
        # AttributeError: 'GetInstgramUsernames' object has no attribute 'reader'
        self.xyz = csv.reader(open('D:\\dev\\scrapy\\instagram_influencers\\instagram_influencers\\input\\user_names.csv','r'))
        print(list(self.xyz))



csv = GetInstgramUsernames()

csv.read()

And the actual console output

python xmen.py
D:\ProgramData\Anaconda2\lib\csv.pyc
[['user_name'], ['mensfashionpost'], ['creativefasion']]
Traceback (most recent call last):
File "xmen.py", line 25, in <module>
    csv.read()
File "xmen.py", line 18, in read
    self.xyz = csv.reader(open('D:\\dev\\scrapy\\instagram_influencers\\instagram_influencers\\input\\user_names.csv','r'))
AttributeError: 'GetInstgramUsernames' object has no attribute 'reader'    D:\dev\scrapy\instagram_influencers>
David Cruwys
  • 6,262
  • 12
  • 45
  • 91

2 Answers2

0

If you want to add anything to the object, you need to use self. So, the line

    reader = csv.reader(open('D:\\dev\\scrapy\\instagram_influencers\\instagram_influencers\\input\\user_names.csv','r'))

should say

    self.reader = csv.reader(open('D:\\dev\\scrapy\\instagram_influencers\\instagram_influencers\\input\\user_names.csv','r'))

You'll notice that you did something similar in the object initialization (the __init__ section).

Mike Williamson
  • 4,915
  • 14
  • 67
  • 104
  • Also, while this didn't cause the error, you'll notice that you fed in the file name and assigned it to a property, but then when you performed `csv.reader` you actually used the raw file name. You'll want to fix that, too, but it wasn't the actual error you were seeing. – Mike Williamson Dec 07 '17 at 03:37
  • Hi Mike, thanks for the reply, buy I was actually just storing to a local variable, I was not trying to add the result on the class, but instead just use the csv reader inside a method, I have altered my code above slightly so that the word reader is not in both the variable and the csv.reader and I still get the same error – David Cruwys Dec 07 '17 at 03:43
0

My mistake was similar to the other questions.

I did not have a file called csv.py but I did use a variable name called csv.

Thanks @user2357112 for pointing this out

David Cruwys
  • 6,262
  • 12
  • 45
  • 91