[Tutor] manipulting CSV files

Emile van Sebille emile at fenx.com
Thu Jan 7 22:30:39 CET 2010


On 1/7/2010 10:26 AM Lowell Tackett said...
I suspect using csv is overkill, but you'd need to be working with 
Dialects by first creating a Dialect, then applying the Dialect to your 
source data.

I've not done that, and for your use case, I think I'd do something like:

filedata = '''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,'''.split('\n')

for ii in filedata:
     print [ jj.strip() for jj in ii.split(',')[:4]]


HTH,

Emile



> 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
>
>
>
>
> _______________________________________________
> Tutor maillist  -  Tutor at python.org
> To unsubscribe or change subscription options:
> http://mail.python.org/mailman/listinfo/tutor




More information about the Tutor mailing list