String object has no attribute "append"

M

Matt Graves

I receive this error while toying around with Functions...

def pulldata(speclist,speccolumn):
with open('profiles.csv', 'r') as f:
reader = csv.reader(f)
for column in reader:
(speclist).append(column[('speccolumn')])

pulldata(speclist = 'numbers', speccolumn = "0")

Traceback (most recent call last):
File "C:\Desktop\Python\CFI\Devices V2\users.py", line 17, in <module>
pulldata(speclist = 'numbers', speccolumn = "0")
File "C:\Desktop\Python\CFI\Devices V2\users.py", line 16, in pulldata
(speclist).append(column[('speccolumn')])
AttributeError: 'str' object has no attribute 'append'

I'm getting the error because it should say "numbers.append", but it is reading it as "(speclist).append".

This is my first time playing with functions, so be gentle.
 
M

marduk

I receive this error while toying around with Functions...

def pulldata(speclist,speccolumn):
with open('profiles.csv', 'r') as f:
reader = csv.reader(f)
for column in reader:
(speclist).append(column[('speccolumn')])

pulldata(speclist = 'numbers', speccolumn = "0")

Traceback (most recent call last):
File "C:\Desktop\Python\CFI\Devices V2\users.py", line 17, in <module>
pulldata(speclist = 'numbers', speccolumn = "0")
File "C:\Desktop\Python\CFI\Devices V2\users.py", line 16, in pulldata
(speclist).append(column[('speccolumn')])
AttributeError: 'str' object has no attribute 'append'

I'm getting the error because it should say "numbers.append", but it is
reading it as "(speclist).append".

Because it indeed says "(speclist).append"... am I missing something?
 
I

Ian Kelly

I receive this error while toying around with Functions...

def pulldata(speclist,speccolumn):
with open('profiles.csv', 'r') as f:
reader = csv.reader(f)
for column in reader:
(speclist).append(column[('speccolumn')])

pulldata(speclist = 'numbers', speccolumn = "0")

Traceback (most recent call last):
File "C:\Desktop\Python\CFI\Devices V2\users.py", line 17, in <module>
pulldata(speclist = 'numbers', speccolumn = "0")
File "C:\Desktop\Python\CFI\Devices V2\users.py", line 16, in pulldata
(speclist).append(column[('speccolumn')])
AttributeError: 'str' object has no attribute 'append'

I'm getting the error because it should say "numbers.append", but it is reading it as "(speclist).append".

This is my first time playing with functions, so be gentle.

It looks like you're trying to pass in a list called 'numbers' into
the pulldata function, but that is not what's happening. Because of
the single quotes you have around numbers, you are passing in the
/string/ 'numbers' and calling it 'speclist' instead. The same goes
for speccolumn as well; I think you mean to pass in the integer 0, but
you're passing in the string "0" instead. Try it without the quotes:

pulldata(speclist = numbers, speccolumn = 0)

And along the same lines, the expression column[('speccolumn')] should
just be column[speccolumn].
 
C

Chris Angelico

I receive this error while toying around with Functions...

def pulldata(speclist,speccolumn):
(speclist).append(column[('speccolumn')])

pulldata(speclist = 'numbers', speccolumn = "0")

I'm getting the error because it should say "numbers.append", but it is reading it as "(speclist).append".

Others have answered the immediate problem, but you may find this a
worthwhile read:

http://mail.python.org/pipermail/tutor/2010-December/080505.html

It's an excellent explanation of the way Python passes arguments to functions.

ChrisA
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,968
Messages
2,570,152
Members
46,698
Latest member
LydiaHalle

Latest Threads

Top