UserGuidelines/GIS/Data: moveWKT.py

File moveWKT.py, 1.0 KB (added by Fran Boon, 11 years ago)

Move WKT column to end of CSV

Line 
1#!/bin/env python
2
3import sys
4
5try:
6 if len(sys.argv) > 1:
7 # Run as python scriptname xxx.csv
8 input = sys.argv[1]
9 else:
10 # Run as ./scriptname xxx.csv
11 input = sys.argv[0]
12except:
13 print "Specify CSV file as argument: python moveWKT.py myfile.csv"
14 sys.exit(2)
15
16try:
17 prefix, extension = input.split(".", 1)
18except:
19 print "Invalid filename!"
20 sys.exit(2)
21
22if extension != "csv":
23 print "Input file should be xxx.csv!"
24 sys.exit(2)
25
26try:
27 inputFile = open(input, "r")
28except:
29 print "Cannot open file!"
30 sys.exit(2)
31
32output = "%s-fixed.csv" % prefix
33outputFile = open(output, "w")
34
35header = True
36for line in inputFile:
37 if header:
38 outputFile.write(line.strip().split(",", 1)[1] + ",WKT\n")
39 header = False
40 continue
41 pieces = line.split('"', 2)
42 wkt = pieces[1]
43 rest = pieces[2][1:].strip()
44 outputFile.write(rest + ',"' + wkt + '"\n')
45
46inputFile.close()
47outputFile.close()