Skip to content

Instantly share code, notes, and snippets.

@tbnorth
Created January 26, 2017 19:39
Show Gist options
  • Select an option

  • Save tbnorth/939c17fca4cbb2e92ecd37cfe00f8d45 to your computer and use it in GitHub Desktop.

Select an option

Save tbnorth/939c17fca4cbb2e92ecd37cfe00f8d45 to your computer and use it in GitHub Desktop.
Make a GDAL csvt file for a csv file
"""
makecsvt.py - make GDAL .csvt file
Terry Brown, [email protected], Thu Aug 04 10:04:14 2016
"""
import csv
import sys
def get_type_text(path):
reader = csv.reader(open(path))
headers = next(reader)
types = [int] * len(headers)
# sys.stderr.write("%s\n" % ','.join(headers))
for row in reader:
for n, i in enumerate(row):
if types[n] == int:
try:
int(i)
except ValueError:
types[n] = float
if types[n] == float:
try:
float(i)
except ValueError:
types[n] = str
return ','.join(
'"%s"'%{int:'Integer', float:'Real', str:'String'}[i]
for i in types
)
def main():
print(get_type_text(sys.argv[1]))
if __name__ == '__main__':
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment