嗨,我想code在它分為notes兩部分configurations和parameters. configurations位于 和 的內部,位于[]大括號notes的左側()。然而parameters,位于大括號內()。對于notes那些parameters我想用逗號將它們分開。如果 aparameter有一個或多個configurations包含所有elements的串列,config則用逗號[分隔element 1, element 2]。對于parameters沒有任何configs創建和空串列 []。如果注釋沒有引數,則parameter和configuration部分都將是型別None. 我想從下面的預期輸出中獲得結果。
代碼:
import re
import pandas as pd
lines = ['yes hello there', 'move on to the next command if the previous command was successful.',
"$$n:describes the '&&' character in the RUN command.",
'k',
'$$n[t(a1), mfc(s,expand,rr), np(), k]: description']
notes = []
parameters= []
configurations= []
for i, line in enumerate(lines):
if re.search(r'\$\$.*\:', line):
notes.append(re.sub(r'\$\$.*\:', '', line).strip())
df = pd.DataFrame({
'Note': notes,
'Parameters': parameters,
'configurations': configurations
})
預期輸出:
---- ------------------------------------------------ -------------- --------------------------
| | Note | Parameters | Configurations |
|---- ------------------------------------------------ -------------- --------------------------|
| 0 | describes the && character in the RUN command. | None | None |
| 1 | description | t,mfc,np,k | [a1],[s,expand,rr],[],[] |
---- ------------------------------------------------ -------------- --------------------------
uj5u.com熱心網友回復:
這將創建子串列:
notes = []
parameters= []
configurations= []
for i, line in enumerate(lines):
expr = re.search(r'\$\$[^:[]*?(?:\[([^:\]]*)\])?\:', line)
if expr:
notes.append(re.sub(r'\$\$.*?\:', '', line).strip())
if expr[1]:
names = []
confs = []
for part in re.findall(r'([^(,] )(?:\(([^)]*)\))?', expr[1]):
names.append(part[0])
confs.append(part[1].split(",") if part[1] else [])
parameters.append(names)
configurations.append(confs)
else:
parameters.append(None)
configurations.append(None)
如果您需要這些值是字串而不是子串列,那么:
notes = []
parameters= []
configurations= []
for i, line in enumerate(lines):
expr = re.search(r'\$\$[^:[]*?(?:\[([^:\]]*)\])?\:', line)
if expr:
notes.append(re.sub(r'\$\$.*?\:', '', line).strip())
if expr[1]:
names = []
confs = []
for part in re.findall(r'([^\s(,] )(?:\(([^)]*)\))?', expr[1]):
names.append(part[0])
confs.append(f"[{part[1]}]")
parameters.append(",".join(names))
configurations.append(",".join(confs))
else:
parameters.append(None)
configurations.append(None)
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/505252.html
上一篇:我試圖根據陣列值java制作一個“圖形”列印一定數量的字符
下一篇:動態命名串列中的專案
