使用pyhton脚本从sql语句中提取部分

问题描述 投票:0回答:1

我有一个 .sql 文件,其中包含 CREATE TABLE 和 INSERTS TABLE 语句。 示例:

INSERT INTO `examle` (`myid`, `title`, `value`, `usefull`, `picture`, `short_description`, `description`, `wikipedia`, `category`, `date`) VALUES
(1, 'Cat', '3,45', 'No', 'cat.jpg', 'A very useless domestic cat.', 'The cat (Felis catus), also known as the domestic cat or housecat to distinguish it from other felines and felids, is a small carnivorous mammal that is valued by humans for its companionship and its ability to hunt vermin and household pests. It has been associated with humans for at least 9,500 years and is currently the most popular pet in the world.', 'http://en.wikipedia.org/wiki/Cat', 'Sport,Food,Creature', '2009-10-27 16:37:49'),
(3, 'Basketball', '9,45', 'Yes', 'basketball.jpg', 'A Baskeball sport utility.', 'Basketball is a team sport in which two teams of 5 players try to score points against one another by placing a ball through a 10 foot (3.048 m) high hoop (the goal) under organized rules. Basketball is one of the most popular and widely viewed sports in the world.', 'http://en.wikipedia.org/wiki/Basketball', 'Sport', '2009-10-27 16:36:39')

我必须在数据库中迁移该数据,其中的表具有与 .sql 结构不同的表。

我的问题是:如何获取文件的所有插入内容以及每个插入内容中分隔的列和值?每个插入应该是列表中的字典。

我尝试使用使用正则表达式和

split(', ')
每一行值的pyhton脚本,但由于某些字段具有
","
,因此很难正确分割值。

python sql file phpmyadmin migration
1个回答
0
投票

这非常脆弱,但会给您一个与您提供的格式相同的语句的字典列表。

鉴于:

statement = """
INSERT INTO `examle` (`myid`, `title`, `value`, `usefull`, `picture`, `short_description`, `description`, `wikipedia`, `category`, `date`) VALUES
(1, 'Cat', '3,45', 'No', 'cat.jpg', 'A very useless domestic cat.', 'The cat (Felis catus), also known as the domestic cat or housecat to distinguish it from other felines and felids, is a small carnivorous mammal that is valued by humans for its companionship and its ability to hunt vermin and household pests. It has been associated with humans for at least 9,500 years and is currently the most popular pet in the world.', 'http://en.wikipedia.org/wiki/Cat', 'Sport,Food,Creature', '2009-10-27 16:37:49'),
(3, 'Basketball', '9,45', 'Yes', 'basketball.jpg', 'A Baskeball sport utility.', 'Basketball is a team sport in which two teams of 5 players try to score points against one another by placing a ball through a 10 foot (3.048 m) high hoop (the goal) under organized rules. Basketball is one of the most popular and widely viewed sports in the world.', 'http://en.wikipedia.org/wiki/Basketball', 'Sport', '2009-10-27 16:36:39')
""".strip().split("\n")

然后你可以使用

ast
包来解析你的语句,如:

import ast

## -------------------------
## Assume the first line is the INSERT clause
## -------------------------
fieldnames = statement[0].replace("`", "'")
fieldnames = ast.literal_eval(" ".join(fieldnames.split(" ")[3:-1]))
## -------------------------

## -------------------------
## Assume remaining lines are data
## -------------------------
data = ast.literal_eval(" ".join(statement[1:]))
## -------------------------

## -------------------------
## Construct our desired result
## -------------------------
results = [dict(zip(fieldnames, row)) for row in data]
## -------------------------

您可以使用

json
包进行格式化来查看结果:

import json

print(json.dumps(results, indent = 4))

这应该给你:

[
    {
        "myid": 1,
        "title": "Cat",
        "value": "3,45",
        "usefull": "No",
        "picture": "cat.jpg",
        "short_description": "A very useless domestic cat.",
        "description": "The cat (Felis catus), also known as the domestic cat or housecat to distinguish it from other felines and felids, is 
a small carnivorous mammal that is valued by humans for its companionship and its ability to hunt vermin and household pests. It has been associated with humans for at least 9,500 years and is currently the most popular pet in the world.",
        "wikipedia": "http://en.wikipedia.org/wiki/Cat",
        "category": "Sport,Food,Creature",
        "date": "2009-10-27 16:37:49"
    },
    {
        "myid": 3,
        "title": "Basketball",
        "value": "9,45",
        "usefull": "Yes",
        "picture": "basketball.jpg",
        "short_description": "A Baskeball sport utility.",
        "description": "Basketball is a team sport in which two teams of 5 players try to score points against one another by placing a ball through a 10 foot (3.048 m) high hoop (the goal) under organized rules. Basketball is one of the most popular and widely viewed sports in the world.",
        "wikipedia": "http://en.wikipedia.org/wiki/Basketball",
        "category": "Sport",
        "date": "2009-10-27 16:36:39"
    }
]
© www.soinside.com 2019 - 2024. All rights reserved.