簡體   English   中英

如果標記在字符內,則將字符串添加到字符串的標記

[英]Add string to tokens of a string if token is within characters

我有以下字符串,

test = "if 'col.A' not null and if 'col.B' not 'null'

我想為封裝在''中的令牌添加str(row[...]並在令牌中包含一個. 。如何在 python 中實現這一點,我不是正則表達式專家。在此之前我是手動替換,但由於''中的數字元素要增加,我想自動化它。我希望最終結果是這樣的,

test = "if str(row['col.A']) not null and if str(row['col.B']) not 'null'

謝謝!

嘗試:

import re

out = re.sub(r"'([^']*)'", r"str(row['\1'])", test)
print(out)

# Output:
"if str(row['col.A']) not null and if str(row['col.B']) not null"

暫無
暫無

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

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