[英]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.