python - 如何将 JSON 数据中的值插入表中

标签 python json psycopg2

我有一个 JSON 数据,

[[{'Design Project': 'Not Available',
   'Design Target*': 'Not Available',
   'Median Property*': '50',
   'Metric': 'ENERGY STAR score (1-100)'},
  {'Design Project': 'Not Available',
   'Design Target*': '35.4',
   'Median Property*': '141.4',
   'Metric': 'Source EUI (kBtu/ft²)'},
  {'Design Project': 'Not Available',
   'Design Target*': '15.8',
   'Median Property*': '63.1',
   'Metric': 'Site EUI (kBtu/ft²)'},
  {'Design Project': 'Not Available',
   'Design Target*': '3,536.0',
   'Median Property*': '14,144.1',
   'Metric': 'Source Energy Use (kBtu)'},
  {'Design Project': 'Not Available',
   'Design Target*': '1,578.7',
   'Median Property*': '6,314.9',
   'Metric': 'Site Energy Use (kBtu)'},
  {'Design Project': 'Not Available',
   'Design Target*': '34.61',
   'Median Property*': '138.44',
   'Metric': 'Energy Cost ($)'},
  {'Design Project': '0.0',
   'Design Target*': '0.2',
   'Median Property*': '0.6',
   'Metric': 'Total GHG Emissions (Metric Tons CO2e)'}],
 [{'Energy Type': ['Energy Not Entered',
                   'Assumed Mix Based on State & Property Type:',
                   '',
                   'Electric - Grid (56.9%)',
                   'Natural Gas (43.1%)'],
   'Target': ' Target % Better than Median: 75',
   'Title': "About this Property's Design",
   'Uses:': 'Other - Education (100.0%)'}],
 [{'Your Design Score ': ' N/A'}]]

我想从 Json 中删除键,因为在我的 Postgres 表中,键是我的列。我想将值插入到我的列中。

我想使用 Python 来完成。

最佳答案

def f(l_of_l_of_ds):
    return [list(l2.values())
            for l1 in l_of_l_of_ds
            for l2 in l1]

其产量:

[['Not Available', 'Not Available', '50', 'ENERGY STAR score (1-100)'],
 ['Not Available', '35.4', '141.4', 'Source EUI (kBtu/ft²)'],
 ['Not Available', '15.8', '63.1', 'Site EUI (kBtu/ft²)'],
 ['Not Available', '3,536.0', '14,144.1', 'Source Energy Use (kBtu)'],
 ['Not Available', '1,578.7', '6,314.9', 'Site Energy Use (kBtu)'],
 ['Not Available', '34.61', '138.44', 'Energy Cost ($)'],
 ['0.0', '0.2', '0.6', 'Total GHG Emissions (Metric Tons CO2e)'],
 [['Energy Not Entered',
   'Assumed Mix Based on State & Property Type:',
   '',
   'Electric - Grid (56.9%)',
   'Natural Gas (43.1%)'],
  ' Target % Better than Median: 75',
  "About this Property's Design",
  'Other - Education (100.0%)'],
 [' N/A']]

关于python - 如何将 JSON 数据中的值插入表中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47465735/

相关文章:

python - 在嵌入式 Python 解释器中启用代码完成

json - json响应末尾出现意外 token }

python - 进行参数化查询并将其封装在python函数中的任何方法

python - Google cloud app.yaml cron.yaml for python 脚本不工作但没有日志

python - Pandas:将索引更改为列

python - 在Python中使用递归函数计算扩展gcd

json - Swift 中的 JSON 文本不显示

c# - 使用日期将 JQuery JSON 发送到 WCF REST

python - 使用 psycopg2 无密码连接到数据库

如果该列的所有值为 NULL,Postgresql 更新具有 NULL 值的数字类型列将失败