Skip to content

Instantly share code, notes, and snippets.

@allenmichael
Last active September 2, 2020 14:47
Show Gist options
  • Save allenmichael/ddb0553308ae0b61737c8222d27e4be6 to your computer and use it in GitHub Desktop.
Save allenmichael/ddb0553308ae0b61737c8222d27e4be6 to your computer and use it in GitHub Desktop.
import csv
def detect_type(t, s):
if s is '':
print(f'{t} is unknown type')
return {t: 'null'}
if s.isdigit():
print(f'{t} is an int')
return {t: 'int'}
else:
try:
num = float(s)
print(f'{t} is an float')
return {t: 'float'}
except ValueError:
pass
if s.lower() in ['true']:
print(f'{t} is a bool')
return {t: 'bool'}
elif s.lower() in ['false']:
print(f'{t} is a bool')
return {t: 'bool'}
else:
print(f'{t} is a string')
return {t: 'string'}
with open('people.csv') as convert_file:
reader = csv.DictReader(convert_file)
examples = next(reader)
convert_file.seek(0)
next(reader)
types = {}
[types.update(d) for d in [detect_type(d, examples[d]) for d in examples]]
print(types)
with open('table_map.csv', 'w') as table_map:
writer = csv.DictWriter(table_map, fieldnames=reader.fieldnames)
writer.writeheader()
writer.writerow(types)
# [detect_type(x) for x in data]
email age isSubscribed firstName lastName score
[email protected] 35 false AM Grobelny 3.5
[email protected] 22 true Fox Mulder 4.3
[email protected] 35 True Dana Scully 5.6
email age isSubscribed firstName lastName score
string int bool string string float
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment