[Tutor] manipulting CSV files

Lowell Tackett lowelltackett at yahoo.com
Thu Jan 7 19:26:18 CET 2010


Displayed below is an extract from a CSV file that displays some [land surveying] coordinates:

1001, 342821.71900, 679492.08300,      0.00000,
1002, 342838.55786, 679909.81375,      0.00000,
1003, 342965.61860, 679911.34762,      0.00000,
1004, 343012.82497, 680338.36624,      0.00000,
1005, 342783.08155, 680347.62727,      0.00000,
1006, 342623.01979, 679547.20429,      0.00000,

I intend to use data such as this along with my Mandrake 10.1 OS and Python 2.5.1 to create some apps that will manipulate coordinate data as I wish.  Obviously, one of my first steps is to be able to discriminatingly access and use the available file data.  (As a reference point of knowledge....I am grasping this effort as a way to introduce myself to programming in general, and Python in particular.  Whatever I absorb/display/offer here will be for the first time ever, and each step will "push my envelop".)

Had originally intended to unpack the data such as:

pt_no, north, east, elev = ('blah, blah')

until I ran across the csv module.  Had accomplished this:

>> coord = csv.reader(open('true_coord'))
>>> for line in coord:
....  print line
....
[' 1001', ' 342821.71900', ' 679492.08300', '      0.00000', '               ']
[' 1002', ' 342838.55786', ' 679909.81375', '      0.00000', '               ']
[' 1003', ' 342965.61860', ' 679911.34762', '      0.00000', '               ']
[' 1004', ' 343012.82497', ' 680338.36624', '      0.00000', '               ']
[' 1005', ' 342783.08155', ' 680347.62727', '      0.00000', '               ']
[' 1006', ' 342623.01979', ' 679547.20429', '      0.00000', '               ']
>>>

when I realized that the procedure had included leading white space.  Attempting to remedy that, I found the Python documentation (on line} and came across--'csv.Dialect.skipinitialspace' which I believe is the answer to my dilemma.  However, my effort to implement that detail, based on interpreting the skimpy examples in the documentation:

>>> coord = csv.reader(open('true_coord'),csv.Dialect.skipinitialspace = True)

got me roundly shot down with:

  File "<stdin>", line 1
SyntaxError: keyword can't be an expression

My seemingly endless stabs at attempting variations on that code line are going nowhere, so I lay my case before the "Council of Coders".


>From the virtual desk of Lowell Tackett  



      
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20100107/0feaa376/attachment.htm>


More information about the Tutor mailing list