简体   繁体   English

如何遍历 python 中的 JSON 列表并插入 PostgreSQL?

[英]How to iterate trough JSON list in python and insert into PostgreSQL?

I am a bit stuck and the solutions I can think of will end in spaghetti code.我有点卡住了,我能想到的解决方案将以意大利面条代码结束。

I am having the following JSON, I create an object of it in python 3 and I have to insert the values into PostgreSQL.我有以下 JSON,我在 python 3 中创建了一个 object,并且我必须将值插入 Z399BD1EE5872845ECAAZ9。

{
    "name": "test",
    "country": "DE",
    "time": "21-Oct-2019 (09:58:01.694763)",
    "toCurrency": ["EUR", "USD", "GBP"],
    "fromCurrency": ["DKK", "DKK", "DKK"],
    "buyMargin": ["1.2800", "1.2800", "2.0000"],
    "sellMargin": ["1.2800", "1.2800", "2.0000"],
    "unit": "M100" 
}

I am getting the exception: can only concatenate str (not "list") to str我得到了例外: can only concatenate str (not "list") to str

  • How can I iterate through those lists in Python and insert one value at a time?如何遍历 Python 中的这些列表并一次插入一个值?

At the same time the other values of 'name', 'country', 'time' should be inserted every time.同时每次都要插入'name'、'country'、'time'的其他值。

Currently I can insert this JSON:目前我可以插入这个 JSON:

{
"name": "test",
"country": "DE",
"time": "21-Oct-2019 (09:58:01.694763)",
"toCurrency": "EUR",
"fromCurrency": "DKK",
"buyMargin": "1.2800",
"sellMargin": "1.2800",
"unit": "M100" 
}

Convert your data to a dataframe using pandas.使用 pandas 将您的数据转换为 dataframe。 Then using sqlalchemy , store this dataframe as a table in PostGre.然后使用sqlalchemy ,将此 dataframe 作为表存储在 PostGre 中。

import pandas as pd
from sqlalchemy import create_engine
d1 = {
    "name": "test",
    "country": "DE",
    "time": "21-Oct-2019 (09:58:01.694763)",
    "toCurrency": ["EUR", "USD", "GBP"],
    "fromCurrency": ["DKK", "DKK", "DKK"],
    "buyMargin": ["1.2800", "1.2800", "2.0000"],
    "sellMargin": ["1.2800", "1.2800", "2.0000"],
    "unit": "M100" 
}
df1 = pd.DataFrame(d1)
engine = create_engine(
        'postgresql://username:password@host:port/database')
df1.to_sql(tablename, engine, if_exists='append', index=False)

Your Dataframe will look as follows.您的 Dataframe 将如下所示。 And will similarly be stored as a table in PostGre并且将类似地存储为 PostGre 中的表

在此处输入图像描述

If I understand correct and your toCurrency , fromCurrency , buyMargin and sellMargin have equal number of elements which they should, the following should work gor you:如果我理解正确并且您的toCurrencyfromCurrencybuyMarginsellMargin具有相同数量的元素,它们应该具有相同数量的元素,那么以下内容应该对您有用:

j = {
    "name": "test",
    "country": "DE",
    "time": "21-Oct-2019 (09:58:01.694763)",
    "toCurrency": ["EUR", "USD", "GBP"],
    "fromCurrency": ["DKK", "DKK", "DKK"],
    "buyMargin": ["1.2800", "1.2800", "2.0000"],
    "sellMargin": ["1.2800", "1.2800", "2.0000"],
    "unit": "M100"
}

for idx, val in enumerate(j['toCurrency']):
    print(j['toCurrency'][idx], j['fromCurrency'][idx], j['buyMargin'][idx], j['sellMargin'][idx])

So your insert statement should be in the position of the print and your columns should be accordingly.所以你的插入语句应该在打印的 position 中,你的列应该是相应的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM