[GIS] select column in csv file in Python

arcmappythonshapefile

I would like to study a range of data using the same technique in ArcMAP (it will take too much time to do it one by one 😉 !). I've been just recently playing with ArcMAP and Python (which can help me to generate an automatic sequence).

I also read that it is easier to import .csv file into ArcMAP.

I'm importing the csv file using csv.reader (just coordinate of points without header, two colums x and y) like:

file = csv.read(...)

Then I didn't managed to print the file, because i obtained this message:

<_csv.writer object at 0x1426F7B0>

And I want to put the first column in the x coordinate and the second column in the y coordinate in a point shapefile?

I read a lot of information how to create file. But I'm stuck in taking the first and second column of my file.

Can you give me some advices?

TA

Best Answer

For this CSV file, simple-csv.csv:

34.79038,-96.80871,"4/13/1983"
34.93032,-96.44490,"2/5/1967"
34.95507,-96.92268,"12/23/2001"
34.95689,-96.92263,"8/9/1999"
34.92559,-96.68021,"8/25/1954"

This code will open it up and print it out:

>>> # import csv module
>>> import csv
>>> # open and read the csv file into memory
>>> file = open(‘C:/testing/simple-csv.csv’)
>>> reader = csv.reader(file)
>>> # iterate through the lines and print them to stdout
>>> # the csv module returns us a list of lists and we
>>> # simply iterate through it
>>> for line in reader:
...     print line
...
[‘34.79038’, ‘-96.80871’, ‘4/13/1983’]
[‘34.93032’, ‘-96.44490’, ‘2/5/1967’]
[‘34.95507’, ‘-96.92268’, ‘12/23/2001’]
[‘34.95689’, ‘-96.92263’, ‘8/9/1999’]
[‘34.92559’, ‘-96.68021’, ‘8/25/1954’]

If you wanted to only get the first and second columns, do something like:

for line in reader:
    print line[0], line[1]
Related Question