Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- def is_number(s):
- try:
- float(s) # for int, long and float
- except ValueError:
- try:
- complex(s) # for complex
- except ValueError:
- return False
- return True
- data = '''Some Class|Type 10|Type 11
- Class 1|0.971|0.968
- Class 2|0.995|0.996
- Class 3|0.970|0.971
- Class 4|0.969|0.970
- Class 5|0.992|0.998
- Class 6|0.992|0.998
- Class 7|0.980|0.984
- Class 8|0.973|0.981
- Class 9|0.969|0.978
- Class 10|0.992|0.998'''
- rows = data.split('\n')
- fields = rows[0].split('|')[1:]
- objs = []
- for row in rows[1:]:
- obj = {}
- for i, col in enumerate(row.split('|')[1:]):
- obj[fields[i]] = float(col) if is_number(col) else col
- objs.append(obj)
- for i, obj in enumerate(objs):
- print '#{}: {}'.format(i, obj)
- ''' Output -----------------------------
- #0: {'Type 10': 0.971, 'Type 11': 0.968}
- #1: {'Type 10': 0.995, 'Type 11': 0.996}
- #2: {'Type 10': 0.970, 'Type 11': 0.971}
- #3: {'Type 10': 0.969, 'Type 11': 0.970}
- #4: {'Type 10': 0.992, 'Type 11': 0.998}
- #5: {'Type 10': 0.992, 'Type 11': 0.998}
- #6: {'Type 10': 0.980, 'Type 11': 0.984}
- #7: {'Type 10': 0.973, 'Type 11': 0.981}
- #8: {'Type 10': 0.969, 'Type 11': 0.978}
- #9: {'Type 10': 0.992, 'Type 11': 0.998}
- ------------------------------------ '''
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement