#1
  1. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2002
    Posts
    0
    Rep Power
    0

    Question Newbie question about list.append


    I'm trying to write some Python code... I'm a newbie, and I just can't figure out what I am doing wrong. I get a "can't assign to function call" error on line 10. Here is the code:

    import string
    import sys


    listhorz = []
    listvert = []

    for line in open('IPs.csv').readlines():
    temp = string.strip(line)
    listhorz(4)= string.split( 'temp' , ',' ) <== line 10
    listvert.append(listhorz)

    I split up my code, to make sure is was the list assignment.

    Any ideas?
    Carrie
  2. #2
  3. Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2001
    Location
    St. George, Utah
    Posts
    63
    Rep Power
    37
    What exactly are you trying to do on the

    Code:
    listhorz(4)= string.split( 'temp' , ',' )
    line? It's the (4) that is confusing me and Python.

    The
    Code:
    listhorz(4)
    part makes Python (and anyone reading your code) think that you are calling a function called listhorz with one parameter: 4.

    With

    Code:
    string.split( 'temp' , ',')
    you're trying to split the string literal 'temp' on the comma character, not the string contained in the variable temp.

    Here's what I think you are trying to do:

    Code:
    import string
    import sys
    
    listhorz = []
    listvert = []
    
    for line in open('IPs.csv').readlines():
        temp = string.strip(line)
        listhorz= string.split( temp, ',' )
        listvert.append(listhorz)
    Am I right?
    Lucas Marshall
  4. #3
  5. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2002
    Posts
    0
    Rep Power
    0
    Yes, that was what I was trying to do. I was trying to create a 2D array, and having a little trouble with the construction, due to my examples being all 1D. I eventually wrote this:

    for line in open('IPs.csv').readlines():
    temp = string.strip(line)
    data.append(string.split( temp , ',' ))


    This seems to work very well, and I cleared out all my other issues, but one.

    I am trying to loop through a list. Essentially, I have a file, and for every line in the file, I need to compare it to the first 2 entries of every line in my 2D array. I'm comparing IP ranges, if that helps. Like this:

    IP IP Region
    IP IP Region
    IP IP Region

    I know I need to use a statement like one of these:

    While listentry is not null

    or

    for entry in list

    But, I'm not able to find a good example.

    Do you have one?

    Thanks,
    Carrie
  6. #4
  7. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2002
    Posts
    0
    Rep Power
    0
    Nevermind, I figured that part out, and went on to another piece of my script.

    Now I'm down to just one syntax error, I think.

    Carrie

IMN logo majestic logo threadwatch logo seochat tools logo