# Keep column headers. Convert numbers to numbers. Empty strings into None (empty "set")
def get_col(col):
if col:
try:
return int(col)
except:
return col
else:
return None
rows = [row.split (',') for row in data.split ('\n')]
# Convert empty columns into None and
rows = [[get_col(col) for col in cols] for cols in rows]
这将导致rows如下所示:
^{pr2}$
替换get_col函数如下:
def get_col(col):
if col:
return col
else:
return 'null'
如前所述,
None
是Python中null的等价物。您可以通过以下方法实现:这将导致
^{pr2}$rows
如下所示:替换get_col函数如下:
将使您
rows
如下所示:(使用Python 2.7.9)
相关问题 更多 >
编程相关推荐