Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to Read CSV files with Python2.3 1

Status
Not open for further replies.

nvwildfire

Technical User
Aug 15, 2002
43
US
I know this is a very simple question, but being a python newbie I need some help (this is my first project trying to use python instead of VB).

I have a CSV file that is exported from a program called terrain navigator, I want to read in the data from the CSV file and then write it out as an ESRI shape file using pyshapelib. I think I've got the usage down for creating a shape file with pyshapelib but I need help getting the data in. It seems very simple to read data but I want to be able to pull specifics out of each row of data in the csv file. The first 2 columns of data (separated by commas) are coordinate pairs (x,y), I need these to create the output format.

So with that in mind can someone give me an example of how to use CSV in the standard module? I am really struggling on this one. Any information or direction would be greatly appreciated.

Thank you in advance.

kgk
 
StoneDeCroze,

Thanks for the reply, here are a few lines of the csv file.

42.23133813, -121.44313522, "maybe1", ff00ff
42.23132888, -121.43319847, "maybe1", ff00ff
42.22792478, -121.43320996, "maybe1", ff00ff
42.22802306, -121.44307129, "maybe1", ff00ff
42.23133715, -121.44306254, "maybe1", ff00ff
42.23133748, -121.44308677, "maybe1", ff00ff

Any examples would be greatly appreciated. Thank you in advance.

nv_wildfire
 
Hi there,

I worked on the basis that the file looked fixed width:

f = file('d:\\tek.txt', 'r').readlines()
for l in f:
x, y, a, b = l[0:11], l[12:26], l[27:36], l[37:-1]
print x, y, a, b

I hope this helps (not altogether sure I have the right end of the problem).
 
Thanks for the code StoneDeCroze, this really helps me get going. Being a python newbie examples that apply to what I am trying to do are very helpfull.

Thanks again.

nv_wildfire
 
This might be more robust...
Code:
  import csv
  reader = csv.reader(file("d:/tek.txt"))
  for row in reader:
      print row
-- bjorn
 
BjornOne,

thanks for the snippet of code. I ended up using the csv module like you show. You get a star for your post.

kgk

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top