4 回答
TA贡献1752条经验 获得超4个赞
我们可以看到每只股票都以 分隔, 。但由于, “,Inc.”也使用相同的字符。,我们需要用 分隔股票名称), 。
让我们使用 Python 的内置函数来分割文本split()。我们从文本中获取字符串列表。像这样:
text = "Alexandria Real Estate Equities, Inc. (ARE), Beyond Meat, Inc. (BYND), Brown & Brown, Inc. (BRO), Canon Inc. (CAJ), Chegg, Inc. (CHGG), Cincinnati Financial Co. (CINF), Ecopetrol SA (EC), Hasbro, Inc. (HAS), HCA Healthcare, Inc. (HCA), HSBC Holdings plc (HSBC), NXP Semiconductors (NXPI), Otis Worldwide (OTIS), Packaging Co. of America (PKG), Petróleo Brasileiro S.A. - Petrobras (PBR), Principal Financial Group Inc (PFG), Principal Financial Group, Inc. (PFG), SAP SE (SAP), Twilio Inc (TWLO)"
split_text = text.split("), ")
输出:
['Alexandria Real Estate Equities, Inc. (ARE', 'Beyond Meat, Inc. (BYND', 'Brown & Brown, Inc. (BRO', 'Canon Inc. (CAJ', 'Chegg, Inc. (CHGG', 'Cincinnati Financial Co. (CINF', 'Ecopetrol SA (EC', 'Hasbro, Inc. (HAS', 'HCA Healthcare, Inc. (HCA', 'HSBC Holdings plc (HSBC', 'NXP Semiconductors (NXPI', 'Otis Worldwide (OTIS', 'Packaging Co. of America (PKG', 'Petróleo Brasileiro S.A. - Petrobras (PBR', 'Principal Financial Group Inc (PFG', 'Principal Financial Group, Inc. (PFG', 'SAP SE (SAP', 'Twilio Inc (TWLO)']
让我们)在末尾添加丢失的字符。
要获取每只股票的指数,请enumerate()在循环中使用该函数。
最终代码:
split_text = text.split("), ")
for idx, stock_name in enumerate(split_text):
print(f"[{idx+1}] {stock_name})")
输出:
[1] Alexandria Real Estate Equities, Inc. (ARE)
[2] Beyond Meat, Inc. (BYND)
[3] Brown & Brown, Inc. (BRO)
[4] Canon Inc. (CAJ)
[5] Chegg, Inc. (CHGG)
[6] Cincinnati Financial Co. (CINF)
[7] Ecopetrol SA (EC)
[8] Hasbro, Inc. (HAS)
[9] HCA Healthcare, Inc. (HCA)
[10] HSBC Holdings plc (HSBC)
[11] NXP Semiconductors (NXPI)
[12] Otis Worldwide (OTIS)
[13] Packaging Co. of America (PKG)
[14] Petróleo Brasileiro S.A. - Petrobras (PBR)
[15] Principal Financial Group Inc (PFG)
[16] Principal Financial Group, Inc. (PFG)
[17] SAP SE (SAP)
[18] Twilio Inc (TWLO)
TA贡献1780条经验 获得超5个赞
我会这样去..
import re
stocks = re.split(r'(?<=\)),\s', stocks_string.replace('\n','')
for index, stock in enumerate(stocks):
print(f'[{index+1}] {stock}'
我使用正则表达式 split 来保留股票名称末尾的右括号。(?<=) 称为“向后查找”,并且仅当右括号位于逗号之前时,整个正则表达式才会匹配逗号。
TA贡献1898条经验 获得超8个赞
如果文本文件始终采用这种格式:“此处为全名(短)”,那么我们可以使用简单的 python 拆分,因为我们知道每个术语之间总是有一个“),”。此拆分将返回您要查找的所有值的数组。
TA贡献1712条经验 获得超3个赞
为了给它你需要的格式,我会这样做:
res = [f"[{index+1}] {i})".replace(", ","").replace("\n","") for index, i in enumerate(text.split(")"))]
res.pop()
output = "".join([f"{i}\n" for i in res])
print(output)
打印出来
[1] Alexandria Real Estate EquitiesInc. (ARE)
[2] Beyond MeatInc. (BYND)
[3] Brown & BrownInc. (BRO)
[4] Canon Inc. (CAJ)
[5] CheggInc. (CHGG)
[6] Cincinnati Financial Co. (CINF)
[7] Ecopetrol SA (EC)
[8] HasbroInc. (HAS)
[9] HCA HealthcareInc. (HCA)
[10] HSBC Holdings plc (HSBC)
[11] NXP Semiconductors (NXPI)
[12] Otis Worldwide (OTIS)
[13] Packaging Co. of America (PKG)
[14] Petróleo Brasileiro S.A. - Petrobras (PBR)
[15] Principal Financial Group Inc (PFG)
[16] Principal Financial GroupInc. (PFG)
[17] SAP SE (SAP)
[18] Twilio Inc (TWLO)
添加回答
举报