473,385 Members | 1,732 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

what is wrong with this line

14
Expand|Select|Wrap|Line Numbers
  1. list[1].extend(data_ncdc[il][3])
it gives me an error that 'str' object has no attribute 'extend'. but I also tried
Expand|Select|Wrap|Line Numbers
  1. list[1].extend(float(data_ncdc[il][3]))
Anybody? THANKS
Dec 1 '09 #1
6 4859
bvdet
2,851 Expert Mod 2GB
You should not use "list" as an identifier. The assignment masks the built-in Python function "list()".

You are receiving the error because "list[1]" is a str object, which has no append() method. Use print statements in your code prior to the error to see what the object is.
Dec 1 '09 #2
jenya56
14
Yes, I did print all of these.....
However, in my program I have:
Expand|Select|Wrap|Line Numbers
  1.  if coord_ind == None:
  2.     ncdc_station.append(data_ncdc[il][3])
  3.  else:
  4.     ncdc_station[coord_ind[0]].extend(data_ncdc[il][3])
In this program append works just fine but then I go to extend it does not. I thought that if extend does not work, append will not either?
Could you please help me to correct this problem. THANKS
Dec 1 '09 #3
bvdet
2,851 Expert Mod 2GB
Please use code tags when posting code.

append() and extend() are list methods and will fail on str objects. It's hard for me to know what is going on with your data.
Expand|Select|Wrap|Line Numbers
  1. >>> a = [1,2,3]
  2. >>> a.extend('a')
  3. >>> a
  4. [1, 2, 3, 'a']
  5. >>> a.extend([1,2,3])
  6. >>> a
  7. [1, 2, 3, 'a', 1, 2, 3]
  8. >>> 's'.extend([3,4,5])
  9. Traceback (most recent call last):
  10.   File "<interactive input>", line 1, in ?
  11. AttributeError: 'str' object has no attribute 'extend'
  12. >>> 's'.append(1)
  13. Traceback (most recent call last):
  14.   File "<interactive input>", line 1, in ?
  15. AttributeError: 'str' object has no attribute 'append'
  16. >>> 
Dec 1 '09 #4
jenya56
14
well, I have array a=['0','1','2','3']. I need now to do the following val1='9', then set
a=['0',['1','9'],'2','3']. Is there a way to do it? Thanks.
Dec 3 '09 #5
Glenton
391 Expert 256MB
Expand|Select|Wrap|Line Numbers
  1. In [5]: a=['0','1','2','3']
  2.  
  3. In [6]: type(a[1])
  4. Out[6]: <type 'str'>
  5.  
  6. In [7]: a[1]=list(a[1])
  7.  
  8. In [8]: a
  9. Out[8]: ['0', ['1'], '2', '3']
  10.  
  11. In [9]: a[1].append('9')
  12.  
  13. In [10]: a
  14. Out[10]: ['0', ['1', '9'], '2', '3']
  15.  
Dec 3 '09 #6
Glenton
391 Expert 256MB
You just need to be clear about what type a variable is. Appending to a string is not valid.
Dec 3 '09 #7

Sign in to post your reply or Sign up for a free account.

Similar topics

4
by: Simom Thorpe | last post by:
Hi, I'm trying to insert a line into a MS access DB using ASP on IIS 5. This is the line: con.execute "INSERT INTO newProds(title,desc,catcode) VALUES ('Champagne Muff Scarf','','AC304B')"...
47
by: Neal | last post by:
Patrick Griffiths weighs in on the CSS vs table layout debate in his blog entry "Tables my ass" - http://www.htmldog.com/ptg/archives/000049.php . A quite good article.
72
by: E. Robert Tisdale | last post by:
What makes a good C/C++ programmer? Would you be surprised if I told you that it has almost nothing to do with your knowledge of C or C++? There isn't much difference in productivity, for...
51
by: WindAndWaves | last post by:
Can anyone tell me what is wrong with the goto command. I noticed it is one of those NEVER USE. I can understand that it may lead to confusing code, but I often use it like this: is this...
22
by: noridotjabi | last post by:
Okay. I'm quite embarased to be asking this but I cannot seem to get files to work. I don't know what the problem is. Is there something wrong with this: (file related) #include <stdio.h>...
2
by: Phil | last post by:
Problem code: using System; using System.Collections.Generic; using System.Text; using System.IO; using System.Diagnostics; namespace Streams { class Program {
2
by: Rod | last post by:
I've been struggling with this thing for 2 days, and after searching the 'net for help, I cannot find what is wrong. We're using Crystal Reports XI Release 2, with Visual Studio .NET 2003 in...
10
by: Enkidu | last post by:
Beginner question, sorry! I am using indexers to access an array of StringBuilders in an instance of a class: Getting: value = board1; Setting: board1 = value1 ;
6
by: Kid Programmer | last post by:
Hello guys. I have a question. What's wrong with my compiler. In a simple number averaging program in a GUI window have way into the project I compile the program and I get the following errors: ...
4
by: hirsh.dan | last post by:
i have a functions that writes information to a file. inside that function i have a line in which i call another function. if this line is executed, nothing is written to the file, but if i remark...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.