Python csv file determine data type -
new student here please excuse ignorance, have searched lot , not found solution problem. needing import csv file mixed data types [ int, float, , string], determine data type, maths on ints , floats.
the problem csv reader converts strings ( or strings?). can try , convert float, , if throws error message know string, how tell if float, program needs determine between two?
i allowed import csv , no others. second first-year python subject, , not sure how this.
edit, found 1 answer seems similar problem, still returns wrong answers, ints usually, not always, still returned string type:
import csv tests = [ # (type, test) (int, int), (float, float), ] def gettype(value): typ, test in tests: try: test(value) return typ except valueerror: print 'value error' continue # no match return str file = open('adult.csv') reader = csv.reader(file) filename = 'output.xml' text = open(filename, 'w') text.write('<?xml version="1.0"?>') text.write('<!doctype summary [') headers = reader.next() in headers: print '<name>' print print '</name>' print '<datatype>' in i[1]: print gettype[a] #for row in fields: # text = row[2] # print type(text) print '</datatype>' #for value in i: # print type(value) print '<!element summary\n\n>' #text.write('<element>')
ok sorry everybody, think found code think work:
determine type of value represented string in python
should have searched harder, although still not reliably giving correct type
Comments
Post a Comment