[英]Python iterate through file and extract specific lines to a list for SQL injection
[英]Python extract lines in specific section of SQL script
我有很多来自sql server的存储过程脚本,我想从中提取自定义编码部分。 我要抢占的区域之前是:
/* **************************************************************************************************************************************************** */
/* * Begin Custom Code ******************************************************************************************************************************** */
/* **************************************************************************************************************************************************** */
然后是这个:
/* **************************************************************************************************************************************************** */
/* * End Custom Code ********************************************************************************************************************************** */
/* **************************************************************************************************************************************************** */
因此脚本看起来像:
/* **************************************************************************************************************************************************** */
/* * Begin Custom Code ******************************************************************************************************************************** */
/* **************************************************************************************************************************************************** */
SELECT col1, col2
FROM [dbo].[table1]
ORDER BY col1
/* **************************************************************************************************************************************************** */
/* * End Custom Code ********************************************************************************************************************************** */
/* **************************************************************************************************************************************************** */
有没有办法读取文件并将这些标记之间的行作为一个长字符串(没有制表符或换行符)写入一个单独的文件中?
假设您可以自己阅读文本,则可以执行以下操作:
(受此答案启发)
chunks = sql.replace('/*', '*/').split('*/')
relevant = [x for x in chunks if not x.startswith('/*')
这是基本思路
inputArray = inputFile.split('*/')
for val in inputArray:
if not val.strip().startswith('/*'):
print(val.strip().split("\n").join(" "))
阅读每一行并在其上剥离。 如果结果行为空或以“ / *”开头,则跳过并读取下一行。 否则将其追加到列表中。
sql = '''/* **************************************************************************************************************************************************** */
/* * Begin Custom Code ******************************************************************************************************************************** */
/* **************************************************************************************************************************************************** */
SELECT col1, col2
FROM [dbo].[table1]
ORDER BY col1
/* **************************************************************************************************************************************************** */
/* * End Custom Code ********************************************************************************************************************************** */
/* **************************************************************************************************************************************************** */
'''
lines = []
for line in sql.splitlines():
line = line.strip()
if not line or line.startswith('/*'):
continue
lines.append(line)
' '.join(lines)
'SELECT col1, col2 FROM [dbo].[table1] ORDER BY col1'
使用正则表达式;
' '.join(line.strip() for line in re.sub(r'/\*.*?\*/','',sql,flags=re.DOTALL).strip().splitlines())
'SELECT col1,col2来自[dbo]。[table1] ORDER BY col1'
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.