[英]Python extract lines in specific section of SQL script
I have a bunch of stored procedure scripts from sql server that I'd like to extract the custom coding section from. 我有很多来自sql server的存储过程脚本,我想从中提取自定义编码部分。 The area I'd like to grab is preceded by this:
我要抢占的区域之前是:
/* **************************************************************************************************************************************************** */
/* * Begin Custom Code ******************************************************************************************************************************** */
/* **************************************************************************************************************************************************** */
and followed by this: 然后是这个:
/* **************************************************************************************************************************************************** */
/* * End Custom Code ********************************************************************************************************************************** */
/* **************************************************************************************************************************************************** */
So the script would look like: 因此脚本看起来像:
/* **************************************************************************************************************************************************** */
/* * Begin Custom Code ******************************************************************************************************************************** */
/* **************************************************************************************************************************************************** */
SELECT col1, col2
FROM [dbo].[table1]
ORDER BY col1
/* **************************************************************************************************************************************************** */
/* * End Custom Code ********************************************************************************************************************************** */
/* **************************************************************************************************************************************************** */
Is there a way to read the file and write the lines in between those markers to a separate file as one long string (no tabs or line breaks)? 有没有办法读取文件并将这些标记之间的行作为一个长字符串(没有制表符或换行符)写入一个单独的文件中?
Assuming you can read the text in yourself, you might do something like this: 假设您可以自己阅读文本,则可以执行以下操作:
(Inspired by this answer ) (受此答案启发)
chunks = sql.replace('/*', '*/').split('*/')
relevant = [x for x in chunks if not x.startswith('/*')
Here is the basic idea 这是基本思路
inputArray = inputFile.split('*/')
for val in inputArray:
if not val.strip().startswith('/*'):
print(val.strip().split("\n").join(" "))
Read each line and do strip on it. 阅读每一行并在其上剥离。 If the resulting line is empty or starts with '/*' skip and read next line.
如果结果行为空或以“ / *”开头,则跳过并读取下一行。 Ohterwise append to a list.
否则将其追加到列表中。
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'
Using regex; 使用正则表达式;
' '.join(line.strip() for line in re.sub(r'/\*.*?\*/','',sql,flags=re.DOTALL).strip().splitlines())
'SELECT col1, col2 FROM [dbo].[table1] ORDER BY col1' 'SELECT col1,col2来自[dbo]。[table1] ORDER BY col1'
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.