Writing List to a csv file in Python


IeraseU
7/5/2016 5:43 am
HOME ยป Web Development

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 a error message that says "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()

Related to : Writing List to a csv file in Python
[python] Writing list changes to file.
by mysql in Web Development
I can't seem to get this, how would I write list items to file? Say for example, I have an existing file, and I want to keep adding stuff to it. How would I do this?
Writing List to a csv file in Python
7/5/2016 5:43 am by IeraseU in Web Development

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 a error message that says "TypeError: list indices must be integers, not tuple" - not sure how to correct this. Also, I thought to


Writing list to array and write it to file (Python)
by Peter Z in Web Development

I guess this is pretty easy, but I can not get it to work. I am having a function giving me a list (1x128). The function is inside a loop (1x32). I want to write all the lists (32x128) from the function into to a file. This is the code:


count = 0
savez = np.zeros((waveforms
Writing from a large list to a text file in Python
by Florian Derudder in Web Development

Something strange happens when I try to write from a ordinary list, containing 648470 string-values, to a text file.


textFile = open('bins.txt', 'w')
for item in bigList:
print item # This prints fine
textFile.write(item)
textFile.cl
Reading from textfile into a list and then writing back to that list Python/CGI
by whatintheworldisthat in Web Development

I am trying to basically get the data from keywords.txt and put it into a list. Then, if the user entered data in the form is not in the list, it gets written to the file and it will take the user to a certain webpage depending on if it is in the list or not. I essentially want the file to be j


print out the on-screen-output from a python program to a file without writing them to a file inside the python program
by Mason McCuskey in Web Development

How to print out the on-screen-output from a python program to a file without writing them to a file inside the python program on Linux ?


I use:


my.py > my.log


but no my.log exist after running my.py, which works well.


Any help will be appreciated


Writing Linked list in Python ?
by Thomas in Web Development
class List(): def __init__(self,values=0): self.numb=values self.linker=self def add(self,values): self=self.linker self.numb=values self.linker=List() def show(self): while self!=self.linker: print self.
python over-writing my list elements
by FuzzyHornet in Web Development

Fairly new to python, very new to python classes. Question is a bit involved. Most appreciative of your patience:


I have a class "Star". It is simple. Attributes x, v, and mass. Another class, Galaxy, has an attribute "stars" which is just a list of star objects:


c
writing data from a python list to csv row-wise
by AdamK47 in Web Development

Using python, i am writing data from a list to a .csv file, row-wise.


Code:


writer=csv.writer(open(filepath,'wb'))
header=['type','id','numberOfUpdates','isPingEnabled','lastUpdated']
length_list=len(header)
i=0
while i!=length_list :
data=
Writing a list to a file and reading the contents back into a list using simplejson
by Andrew in Web Development

I would like to write a list to a file and read back the contents of the file into a list.
I am able to write the list to the file using simplejson as follows:


f = open("data.txt","w")
l = ["a","b","c"]
simplejson.dump(l,f)
f.close()

Now to


Privacy Policy - Copyrights Notice - Feedback - Report Violation - RSS 2014 © bighow.org All Rights Reserved .