0

I'm currently using Python v2.10. I'm having trouble writing data to a csv file. Wondering if someone might be able to tell me what I am doing wrong. I'm getting the following error: "TypeError: list indices must be integers, not tuple" - not sure how to correct this. Also, I thought to write to a csv file, the data must be in string format ?

Ideally I would like each row to be like: Name, X1, Y1, X2, Y2, X3, Y3

Here is the code I have:

import csv

def main():
    my_coords = open('Geo_Coords.csv', 'a')
    coords = csv.reader(my_coords)

    how_many = raw_input("How many Geometries do you wish to enter? ")
    counter = 0
    coords = []

    while counter < how_many:
        geometry = raw_input("Geometry Name  ")
        first_coordE = raw_input("1st Co-ord (Easting) ")
        first_coordN = raw_input("1st Co-ord (Northing) ")
        sec_coordE = raw_input("2nd Co-ord (Easting) ")
        sec_coordN = raw_input("2nd Co-ord (Northing) ")
        third_coordE = raw_input("3rd Co-ord (Easting) ")
        third_coordN = raw_input("3rd Co-ord (Northing) ")
        counter = counter + 1

        my_coords.write(coords[[geometry],[first_coordE,first_coordN], [sec_coordE,sec_coordN], [third_coordE,third_coordN]] )

    my_coords.close()
    print my_coords

main()
1
  • The problem seems to be the line coords[[geometry],[first_coordE,first_coordN], [sec_coordE,sec_coordN], [third_coordE,third_coordN]]. You're creating a tuple [geometry],[first_coordE,first_coordN], [sec_coordE,sec_coordN], [third_coordE,third_coordN], then using that to index the list coords. What were you trying to do with that tuple/list of lists? Commented May 7, 2016 at 9:03

2 Answers 2

2

You code contains some issues, you reassign coords as a list before that it was an csv object, you are using csv.reader instead of csv.writer, I encourage you to read the documentation there: https://docs.python.org/2/library/csv.html

Maybe this could help you, I keep the structure you use, I just modified a little bit.

import csv

def main():
    with open('Geo_Coords.csv', 'a') as my_coords:
        coords = csv.writer(my_coords)

        how_many = int(raw_input("How many Geometries do you wish to enter? "))
        # TODO: Check user input
        rows = []

        for _ in range(how_many):
            geometry = raw_input("Geometry Name  ")
            first_coordE = raw_input("1st Co-ord (Easting) ")
            first_coordN = raw_input("1st Co-ord (Northing) ")
            sec_coordE = raw_input("2nd Co-ord (Easting) ")
            sec_coordN = raw_input("2nd Co-ord (Northing) ")
            third_coordE = raw_input("3rd Co-ord (Easting) ")
            third_coordN = raw_input("3rd Co-ord (Northing) ")

            rows.append([geometry, first_coordE,
                         first_coordN, sec_coordE,
                         sec_coordN, third_coordE,
                         third_coordN])

        coords.writerows(rows)


if __name__ == "__main__":
    main()
Sign up to request clarification or add additional context in comments.

Comments

2
import csv

def main():
    my_coords = open('Geo_Coords.csv', 'a')
    coords = csv.reader(my_coords)

OK, coords is the name of a csv.reader() object

    how_many = raw_input("How many Geometries do you wish to enter? ")

and how many is the name of a string, say "15"

    counter = 0
    coords = []

here you do a strange thing, coords is now the name of an empty list and the csv.reader() has no more a name that references it...

    while counter < how_many:

also the above statement is strange... you are comparing an int with a string! in Python 2 the logical expression string < integer is always True!

        geometry = raw_input("Geometry Name  ")
        first_coordE = raw_input("1st Co-ord (Easting) ")
        first_coordN = raw_input("1st Co-ord (Northing) ")
        sec_coordE = raw_input("2nd Co-ord (Easting) ")
        sec_coordN = raw_input("2nd Co-ord (Northing) ")
        third_coordE = raw_input("3rd Co-ord (Easting) ")
        third_coordN = raw_input("3rd Co-ord (Northing) ")
        counter = counter + 1

so far, so good — but note that you've not modified the value of coords, and hence when you'll use the name coords you are just referencing an empty list

        my_coords.write(coords[[geometry],[first_coordE,first_coordN], [sec_coordE,sec_coordN], [third_coordE,third_coordN]] )

and here you are, referencing, by addressing, the content of an empty list; further, the syntax of the addressing is wrong, it must be either an integer or a slice object — further, the .write(...) method expects a string and you are trying, afaict, to write a list of strings at once...

    my_coords.close()
    print my_coords

This will print something like

<closed file 'Geo_Coords.csv', mode 'a' at 0x7efc8946a540>

The answer by Koffee seems totally adequate to me, so I recommend that you accept it and possibly upvote it.

3 Comments

Good and complete explanation @gboffi (y)
@Sнаđошƒаӽ rolled back your edit because the further indent is not an error, it exactly reflects the correct indentatation of OP's code.
Excuse me! I see now that you have explained the code part by part, so your indentation makes it simply better. Sorry that I didn't notice that before. And thank you for notifying me of that!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.