如何将SQLite列中的分隔值拆分为多个列

时间:2016-09-22 21:02:48

标签: python sqlite

如何解析Fruit Basket中的逗号分隔值并将其移至其他列。

例如,我想要这个

Fruit Basket  Fruit1    Fruit2    Fruit3
------------  --------  --------  --------
Apple
Banana, Pear
Lemon, Peach, Apricot

成为这个

Fruit Basket  Fruit1    Fruit2    Fruit3
------------  --------  --------  --------
Apple         Apple
Banana, Pear  Banana    Pear
Lemon, Pea... Lemon     Peach      Apricot

如果我不能用纯SQLite语句执行此操作,我该如何使用Python?

4 个答案:

答案 0 :(得分:0)

查看手册页:

man sqlite3 | less +/-csv

然后使用

sqlite ... -csv | ...

输出将更容易解析

答案 1 :(得分:0)

对于Python来说,拆分一列非常简单(不确定SQLite)。这会将您的数据库行简化为字符串数组,并且应该与SQLite返回类似。

text = [
    'Apple',
    'Banana, Pear',
    'Lemon, Peach, Apricot'
]

for line in text:
    cols = [c.strip() for c in line.split(',')]
    print(cols)

应为每个字符串行输出一个数组:

['Apple']
['Banana', 'Pear']
['Lemon', 'Peach', 'Apricot']

修改

这是一个完整的Python脚本,用于执行您在SQLite上寻找的内容:

import sqlite3

conn = sqlite3.connect('test.db')
c = conn.cursor()
c.execute(
    '''SELECT *
            FROM Fruits
            WHERE Fruit_Basket IS NOT NULL'''
)
rows = c.fetchall()
for row in rows:
    fruit_basket = row[0]
    fruits = [f.strip() for f in fruit_basket.split(',')]
    while (len(fruits) < 3):
        fruits.append('')
    print(fruits)
    update = '''UPDATE Fruits
                    SET Fruit1 = ?, Fruit2 = ?, Fruit3 = ?
                    WHERE Fruit_Basket = ?'''
    c.execute(update, fruits + [fruit_basket,])
conn.commit()
conn.close()

答案 2 :(得分:0)

搞定了

from framework.page_object_model.home_page import HomePage
from robot.api.deco import keyword

class test_create_campaign:
    @keyword(name='I log into Box as my ${user} with ${userpass}')
    def test_create_campaign(self, user, userpass):
        print user
        print userpass

答案 3 :(得分:0)

我遇到了一个类似的问题,并且我开发了一个纯SQL解决方案:

--------------------------------------------------
-- Select fruits split into separate columns 
-- Inspired by this article: https://www.vivekkalyan.com/splitting-comma-seperated-fields-sqliteWITH const AS (SELECT 'name' AS name, 10 AS more)
WITH RECURSIVE split(basket_id, fruitCol, str, fruitColNum) AS ( 
WITH const AS (SELECT ', ' AS delimiter)
    SELECT fruit.ROWID, '', Fruit_basket||delimiter, 0 FROM fruit, const
    UNION ALL SELECT
    basket_id,
    substr(str, 0, instr(str, delimiter)),
    substr(str, instr(str, delimiter) + length(delimiter)),
    fruitColNum+1
    FROM split,const WHERE str!=''
) 
SELECT 
      fruit.Fruit_basket, 
      split1.fruitCol as fr_1,
      split2.fruitCol as fr_2,
      split3.fruitCol as fr_3
      -- ...
FROM fruit 
LEFT JOIN (SELECT * FROM split WHERE fruitColNum=1) as split1 ON fruit.ROWID = split1.basket_id
LEFT JOIN (SELECT * FROM split WHERE fruitColNum=2) as split2 ON fruit.ROWID = split2.basket_id
LEFT JOIN (SELECT * FROM split WHERE fruitColNum=3) as split3 ON fruit.ROWID = split3.basket_id
-- ...
   ;

此SELECT的结果是

Fruit_basket            fr_1    fr_2    fr_3
-------------           -----   -----   -----
Apple                   Apple       
Banana, Pear            Banana  Pear    
Lemon, Peach, Apricot   Lemon   Peach   Apricot