1 | #!/bin/env python
|
---|
2 |
|
---|
3 | import sys
|
---|
4 |
|
---|
5 | try:
|
---|
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]
|
---|
12 | except:
|
---|
13 | print "Specify CSV file as argument: python moveWKT.py myfile.csv"
|
---|
14 | sys.exit(2)
|
---|
15 |
|
---|
16 | try:
|
---|
17 | prefix, extension = input.split(".", 1)
|
---|
18 | except:
|
---|
19 | print "Invalid filename!"
|
---|
20 | sys.exit(2)
|
---|
21 |
|
---|
22 | if extension != "csv":
|
---|
23 | print "Input file should be xxx.csv!"
|
---|
24 | sys.exit(2)
|
---|
25 |
|
---|
26 | try:
|
---|
27 | inputFile = open(input, "r")
|
---|
28 | except:
|
---|
29 | print "Cannot open file!"
|
---|
30 | sys.exit(2)
|
---|
31 |
|
---|
32 | output = "%s-fixed.csv" % prefix
|
---|
33 | outputFile = open(output, "w")
|
---|
34 |
|
---|
35 | header = True
|
---|
36 | for 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 |
|
---|
46 | inputFile.close()
|
---|
47 | outputFile.close()
|
---|