簡體   English   中英

Python 轉 function

[英]Python re.sub function

我需要將大量視圖查詢轉換為 BigQuery 語法。 我的 python 腳本在用 re.sub 替換代碼片段方面效果很好。 我需要刪除包含“LOCKING ROW FOR ACCESS”的行這有什么問題:

file_content = re.sub(r'LOCKING ROW FOR ACCESS', '', file_content)

您並沒有刪除整行,只是刪除了該行的那一部分。

file_contents = re.sub(r'.*LOCKING ROW FOR ACCESS.*', '', file_content)

.*將匹配字符串周圍的任何內容,但不包括換行符。

re.sub只會刪除與正則表達式匹配的file_content部分。 例如,

>>> import re
>>> file_content = 'asdfas LOCKING ROW FOR ACCESSasdfasdf'
>>> file_content = re.sub(r'LOCKING ROW FOR ACCESS', '', file_content)
>>> file_content
'asdfas asdfasdf'

哦,

>>> file_content = 'asdfas LOCKING ROW FOR ACCESSasdfasdf'
>>> file_content = re.sub(r'.*LOCKING ROW FOR ACCESS.*', '', file_content)
>>> file_content
''

這是因為在開頭添加.*將匹配從行首開始的所有內容,對於字符串末尾的.*也是如此。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM