【发布时间】:2013-02-08 22:00:48
【问题描述】:
我有一个 Python 列表,其结构如下:
apts = [ [2083, \
[ ["price", "$1000 / month"], \
["sq ft.", "500"], \
["amenities", "gym hardwood floor"]]], \
[1096, \
[ ["price", "$1200 / month"], \
["sq ft.", "700"], \
["a/c", "true"]]], \
[76, \
[ ["price", "$1100 / month"], \
["Pets", "true"], \
["a/c", "true"]]]]
我如何以一种可以轻松将其传输到 mysql 数据库的格式获取它?基本上,我想重新排列它,使其类似于易于传输的表格/csv文件,例如:
id, price, sq ft, amenities, a/c, pets
2083, $1000 / month, 500, gym hardwood floor, ,
1096, $1200 / month, 700, , true,
76, $1100 / month, , true, true
提前致谢。我可以想办法将这些数据一块一块地映射出来,但似乎效率很低,而且我对 python 的了解也很薄弱,所以我希望有其他快速的方法来转换这些数据...
如果我使用嵌套字典结构而不是嵌套列表会有帮助吗?
【问题讨论】:
-
这是一个列表有什么原因吗?看起来它对我来说应该是一个字典。
-
我对 python 的了解还不够,但是是的,我绝对应该为这个应用程序使用字典。谢谢!
标签: python database list dictionary nested