Created
January 26, 2017 19:39
-
-
Save tbnorth/939c17fca4cbb2e92ecd37cfe00f8d45 to your computer and use it in GitHub Desktop.
Make a GDAL csvt file for a csv file
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| """ | |
| 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