为了账号安全,请及时绑定邮箱和手机立即绑定

如何将“段落”变成列表

如何将“段落”变成列表

不负相思意 2024-01-04 09:43:43
我提前道歉,因为我不知道如何正确地问这个问题。每周我都会收到有关股票更新的收益报告。他们看起来像这样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)由于此消息是通过电子邮件发送的,因此我想找到一种方法将其存储在.txt看起来明显有吸引力的文件中。如果可能的话,我想让它看起来像[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)等等。尽管我不断思考并提出不同的选择,但我仍然陷入困境,不知道如何解决这个问题。任何帮助将不胜感激。
查看完整描述

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)


查看完整回答
反对 回复 2024-01-04
?
翻阅古今

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 来保留股票名称末尾的右括号。(?<=) 称为“向后查找”,并且仅当右括号位于逗号之前时,整个正则表达式才会匹配逗号。


查看完整回答
反对 回复 2024-01-04
?
汪汪一只猫

TA贡献1898条经验 获得超8个赞

如果文本文件始终采用这种格式:“此处为全名(短)”,那么我们可以使用简单的 python 拆分,因为我们知道每个术语之间总是有一个“),”。此拆分将返回您要查找的所有值的数组。



查看完整回答
反对 回复 2024-01-04
?
交互式爱情

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)


查看完整回答
反对 回复 2024-01-04
  • 4 回答
  • 0 关注
  • 106 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信