12.4. Serialization Normalization
12.4.1. Sequence
Mark,Watney,41
def normalize(value, types=(int, float, str)):
if value == 'True':
return True
if value == 'False':
return False
if value == 'None':
return None
for typ in types:
try:
return typ(value)
except ValueError:
continue
data = 'Mark,Watney,41'
values = data.split(',')
result = [normalize(x) for x in values]
print(result)
['Mark', 'Watney', 41]
12.4.2. Mapping
firstname,lastname,age
Mark,Watney,41
data = """firstname,lastname,age
Alice,Apricot,25
"""
12.4.3. List of Sequence
firstname,lastname,age
Mark,Watney,41
Melissa,Lewis,40
Rick,Martinez,39
Alex,Vogel,40
Chris,Beck,36
Beth,Johanssen,29
12.4.4. List of Mappings
age,firstname,lastname
41,Mark,Watney
40,Melissa,Lewis
39,Rick,Martinez
40,Alex,Vogel
36,Chris,Beck
29,Beth,Johanssen
12.4.5. List of Instances
age,firstname,lastname
41,Mark,Watney
40,Melissa,Lewis
39,Rick,Martinez
40,Alex,Vogel
36,Chris,Beck
29,Beth,Johanssen