Last active
August 29, 2015 14:23
-
-
Save tchajed/93c347aeb2b24f034ea3 to your computer and use it in GitHub Desktop.
Parse a CSV file of coordinates in Python
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
1 | 2 | 3 | |
---|---|---|---|
3 | 4 | 5 | |
12.0 | 3.4 | 1.7 | |
4 | 5.4 | 2 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Output from parsing test.csv: