2016-05-07 10 views
0

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() 
+0

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? –

Antwort

2

Sie Code einige enthält Fragen Sie coords als Liste neu zuweisen, bevor dass es ein csv Objekt war, Sie csv.reader statt csv.writer verwenden, möchte ich Sie ermutigen die Dokumentation dort zu lesen: https://docs.python.org/2/library/csv.html

Vielleicht co Ich helfe dir, ich behalte die Struktur, die du benutzt, ich habe nur ein bisschen modifiziert.

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() 
2
import csv 

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

OK, coords ist der Name eines csv.reader() Objekt

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

und how many der Name einer Zeichenkette ist, sagen "15"

counter = 0 
    coords = [] 

hier gibt es eine seltsame tun Ding, coords ist jetzt der Name eines em pty Liste und die csv.reader() hat nicht mehr einen Namen, es verweist ...

while counter < how_many: 

auch die obige Aussage seltsam ist ... Sie sind ein int mit einem String zu vergleichen! In Python 2 ist der logische Ausdruck string < integer immer 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 weit, so gut - aber beachten Sie, dass Sie nicht den Wert von coords geändert haben, und daher, wenn Sie den Namen coords Sie nur verwenden werden Referenzierung eine leere Liste

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

und hier sind Sie, indem Sie den Inhalt einer leeren Liste referenzieren; Außerdem ist die Syntax der Adressierung falsch, es muss entweder eine Ganzzahl oder ein Objekt sein. - außerdem erwartet die .write(...) Methode eine Zeichenkette und Sie versuchen, afaict, eine Liste von Zeichenketten gleichzeitig zu schreiben ...

my_coords.close() 
    print my_coords 

Das wird so etwas wie

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

The answer von Koffee scheint völlig ausreichend, um mich drucken, so empfehle ich, dass Sie es akzeptieren und es möglicherweise upvote.

+0

Gute und vollständige Erklärung @gboffi (y) –

+1

@ Snađошƒаӽ Rollback Ihre Bearbeitung, da der weitere Einzug kein Fehler ist, spiegelt _ _exactly_ die _correct_ Einprägung von OP-Code. – gboffi

+0

Entschuldigung! Ich sehe jetzt, dass Sie den Code Teil für Teil erklärt haben, also macht Ihr Einzug es einfach besser. Tut mir leid, dass ich das vorher nicht bemerkt habe. Und danke, dass du mich darüber informiert hast! –