Skip to content

Instantly share code, notes, and snippets.

@tchajed
Last active August 29, 2015 14:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tchajed/93c347aeb2b24f034ea3 to your computer and use it in GitHub Desktop.
Save tchajed/93c347aeb2b24f034ea3 to your computer and use it in GitHub Desktop.
Parse a CSV file of coordinates in Python
import csv
""" Read a CSV file of points.
Assumes each line of the file is a CSV list of coordinates, which are parsed as
floats.
"""
def read_coords(fname):
with open(fname, 'rb') as f:
points = []
# note that csv.reader has many options to control formatting
# (see https://docs.python.org/2/library/csv.html#csv-fmt-params)
r = csv.reader(f)
for row in r:
point = [float(v) for v in row]
points.append(point)
return points
1 2 3
3 4 5
12.0 3.4 1.7
4 5.4 2
@tchajed
Copy link
Author

tchajed commented Jun 25, 2015

Output from parsing test.csv:

>>> import coords
>>> coords.read_coords("test.csv")
[[1.0, 2.0, 3.0], [3.0, 4.0, 5.0], [12.0, 3.4, 1.7], [4.0, 5.4, 2.0]]

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment