Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/280.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
将Python列表格式化为SQL脚本?_Python - Fatal编程技术网

将Python列表格式化为SQL脚本?

将Python列表格式化为SQL脚本?,python,Python,我有一个列表需要格式化为SQL脚本 list = [['11', ' 0', " 'MMB'", " '2 MB INTERNATIONAL'", ' NULL', ' NULL', ' 0\n'], ['12', ' 0', " '3D STRUCTURES'", " '3D STRUCTURES'", ' NULL', ' NULL', ' 0\n'], ['13', ' 0', " '2 STRUCTURES'", " '2D STRUCTURES'", '

我有一个列表需要格式化为SQL脚本

list = 
    [['11', ' 0', " 'MMB'", " '2 MB INTERNATIONAL'", ' NULL', ' NULL', ' 0\n'], 
     ['12', ' 0', " '3D STRUCTURES'", " '3D STRUCTURES'", ' NULL', ' NULL', ' 0\n'],
     ['13', ' 0', " '2 STRUCTURES'", " '2D STRUCTURES'", ' NULL', ' NULL', ' 0\n'],
要创建如下sql脚本:

INSERT INTO `Tbl_ABC` VALUES (11, 0, 'MMB', '2 MB INTERNATIONAL', NULL, NULL, 0)
INSERT INTO `Tbl_ABC` VALUES (12, 0, '3D STRUCTURES', '3D STRUCTURES', NULL, NULL, 0)
INSERT INTO `Tbl_ABC` VALUES (13, 0, '2 STRUCTURES', '2D STRUCTURES', NULL, NULL, 0)
# retrieve each row from the company table and print it :-O
for company in session.query(Company):
   print company.name + " " + company.address

# add a new company to the company table:
company = Company("Fred Flintstone", "The Quarry")
session.add(company)
session.commit()
这个我已经试过了

import pickle
import re
#RX = re.compile(r'^.*?\(\d+,\s0,.*\s0\)\s*$')

outfile = open('destination.sql', 'wb')
data = []
for ln in open('source.sql', 'r').xreadlines():
    replace1 = ln.replace("INSERT INTO `Tbl_ABC` VALUES (", "")
    replace2 = replace1.replace(")", "")
    list_replace = replace2.split(',')
    data.append(list_replace) 
destinationdata = [d for d in data if d[1] == ' 0' and d[6]==' 0\n']#print '%s ,%s' % (list_replace[1], list_replace[6])

    #start write line to destination.sql
     #if RX.match(ln):        

pickle.dump(destinationdata, outfile)
outfile.close()
谢谢你的帮助

Iterate over each element in the list
  Unpack the element (which is also a list) into its fields
  Generate a SQL line from these fields
完成工作最简单、最丑陋的方式是:

list = [
    ['11', ' 0', " 'MMB'", " '2 MB INTERNATIONAL'", ' NULL', ' NULL', ' 0'], 
    ['12', ' 0', " '3D STRUCTURES'", " '3D STRUCTURES'", ' NULL', ' NULL', ' 0'],
    ['13', ' 0', " '2 STRUCTURES'", " '2D STRUCTURES'", ' NULL', ' NULL', ' 0']]

for elem in list:
    print 'INSERT INTO \'Tbl_ABS\' VALUES (%s, %s, %s, %s, %s, %s, %s)' % tuple(elem)
注意:我清理了0后面的“\n”(如果需要,请相应地进行调整)。
这对于一次性脚本来说是可以的。对于更重要的报告和转换,请查看一些模板库以分离数据和演示文稿。

基本上,这应该是可行的

print "\n".join(["INSERT INTO `Tbl_ABC` VALUES ("+",".join(x).strip()+")" for x in list1])
结果

INSERT INTO `Tbl_ABC` VALUES (11, 0, 'MMB', '2 MB INTERNATIONAL', NULL, NULL, 0)
INSERT INTO `Tbl_ABC` VALUES (12, 0, '3D STRUCTURES', '3D STRUCTURES', NULL, NULL, 0)
INSERT INTO `Tbl_ABC` VALUES (13, 0, '2 STRUCTURES', '2D STRUCTURES', NULL, NULL, 0)
如果有细微差别,请根据您的需要进行调整


ps:我故意在代码中将list更改为list1,因为重写内置函数不是一个好主意。

从技术上讲,手动获取列表并将其转换为sql是相当容易的,但是。。。。你可以考虑看看SQLAlchemy。是的,我感觉非常棒。它将为您完成从Python对象到数据库表行的所有转换,并可以从表中检索Python对象

您可以这样做:

INSERT INTO `Tbl_ABC` VALUES (11, 0, 'MMB', '2 MB INTERNATIONAL', NULL, NULL, 0)
INSERT INTO `Tbl_ABC` VALUES (12, 0, '3D STRUCTURES', '3D STRUCTURES', NULL, NULL, 0)
INSERT INTO `Tbl_ABC` VALUES (13, 0, '2 STRUCTURES', '2D STRUCTURES', NULL, NULL, 0)
# retrieve each row from the company table and print it :-O
for company in session.query(Company):
   print company.name + " " + company.address

# add a new company to the company table:
company = Company("Fred Flintstone", "The Quarry")
session.add(company)
session.commit()

我想不出那些尖刻的评论和答案。这似乎是一个有效的问题,可能缺少英文散文。这是一个比其他许多问题更符合逻辑的问题SO@eliben:最初的问题只是要求python脚本执行特定任务。如果python显示他们已经以任何方式尝试过这一点,并且正在寻求帮助,那么我完全同意。您只需给出一行说明方法。:)感谢您提供的信息。我所做的是将数据库从MS access迁移到SQL。从access获取数据并插入到MySQL,条件如下:)