Read CSV and Column Separation

Brand new for Python (and programming in general), if it is simply and / or answered somewhere that I did not find, feel free to bother me in a typical forum form.

I have a bunch of CSVs, each of which contains 10 XY coordinates:

10,5 2,4 5,6 7,8 9,12 3,45 2,4 6,5 0,3 5,6 

I want to separate the X coordinates and Y coordinates into two separate lists so that I can subtract the value from each value in this list. For example, subtracting 5 from each value in the X coordinate list and 3 from each value in the Y coordinate list. Then I'm going to take the abs () of each value and find the minimum. Once these minima are found, I want to add the lists together so that each value is added to it.

IE) if the absolute values ​​of X were something like

 4 5 .... 

and Y is something like

 6 7 .... 

I would like to add 4 and 6, then 5 and 7, etc.

To separate them, I tried

 import csv filein = open("/path/here") reader = csv.reader(filein, skipinitialspace = True) listofxys = [] for row in reader: listofxys.append(row) Xs = listofxys.pop(0) # to pop all the X's Ys = listofxys.pop() # to pop all the Y's 

But instead of all leading values, it provides the first pair of XY. What am I doing wrong here?

The ultimate goal is to find the closest point to the XY coordinate, so if this is a bad way to do this, feel free to direct me in the other direction.

Thanks in advance!

+4
source share
3 answers

It's worth noting that you should try using the with statement when opening files in Python. This is more readable and removes the possibility that the file remains open (even if exceptions occur).

Your real problem is that you are not doing what you want.

 reader = csv.reader(filein, skipinitialspace = True) listofxys = [] for row in reader: listofxys.append(row) 

All this makes reader = list(csv.reader(filein, skipinitialspace = True)) very inefficient.

What you want to do is use zip() builtin to take a list of pairs and turn it into two lists. You do this with the star operator:

 import csv with open("test") as filein: reader = csv.reader(filein, skipinitialspace = True) xs, ys = zip(*reader) print(xs) print(ys) 

What gives:

 ('10', '2', '5', '7', '9', '3', '2', '6', '0', '5') ('5', '4', '6', '8', '12', '45', '4', '5', '3', '6') 

Note that these values ​​are strings. If you want to have them as numbers, you will want to use csv.QUOTE_NONNUMERIC , for example: reader = csv.reader(filein, quoting=csv.QUOTE_NONNUMERIC, skipinitialspace = True)

What gives:

 (10.0, 2.0, 5.0, 7.0, 9.0, 3.0, 2.0, 6.0, 0.0, 5.0) (5.0, 4.0, 6.0, 8.0, 12.0, 45.0, 4.0, 5.0, 3.0, 6.0) 
+5
source

It looks like you are looking for the zip function described here:

http://docs.python.org/library/functions.html#zip

+1
source
  import os,csv, numpy, scipy from numpy import * f= open('some.csv', 'rb') reader = csv.reader(f, delimiter=',') header = reader.next() zipped = zip(*reader) print( zipped[1] ) # is the 2nd column of the csv file 

NTN

+1
source

All Articles