Ich verwende derzeit Python v2.10. Ich habe Probleme beim Schreiben von Daten in eine CSV-Datei. Ich frage mich, ob jemand mir sagen könnte, was ich falsch mache. Ich erhalte den folgenden Fehler: "TypeError: list indices must be integers, not tuple"
- nicht sicher, wie Sie dies korrigieren. Außerdem dachte ich, in eine CSV-Datei schreiben, müssen die Daten im String-Format sein?Liste in eine Csv-Datei in Python schreiben
Idealerweise würde ich jede Zeile mag wie zu sein: Namen, X1, Y1, X2, Y2, X3, Y3
Hier ist der Code ich habe:
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()
Das Problem scheint die Linie 'coords [[Geometrie], [first_coordE, first_coordN], [sec_coordE, sec_coordN], [third_coordE, third_coordN]]' zu sein.Sie erstellen ein Tupel '[Geometrie], [first_coordE, first_coordN], [sec_coordE, sec_coordN], [third_coordE, third_coordN]', und verwenden das, um die Liste 'coords' zu indizieren. Was hast du mit dem Tupel/Listenverzeichnis versucht? –