gpt4 book ai didi

python - 正则表达式/Python : Find everything before one symbol, 如果它在另一个符号之后

转载 作者:太空宇宙 更新时间:2023-11-04 00:23:08 25 4
gpt4 key购买 nike

如果有一个长破折号 ("―"),则返回完整的字符串,如果为真,则返回第一个逗号 (",") 之前的所有内容。我如何使用 Python 和 Regex 来做到这一点?

from bs4 import BeautifulSoup
import requests
import json
import pandas as pd

request = requests.get('https://www.goodreads.com/quotes/tag/fun?page=1')
soup = BeautifulSoup(request.text, 'lxml')
# for loop
s = soup.find_all("div", class_="quoteText")[0].text
s = " ".join(s.split())
s[:s.index(",")]
s

原始输出:

“That does it," said Jace. "I\'m going to get you a dictionary for Christmas this year.""Why?" Isabelle said."So you can look up \'fun.\' I\'m not sure you know what it means.” ― Cassandra Clare, City of Ashes //<![CDATA[ function submitShelfLink(unique_id, book_id, shelf_id, shelf_name, submit_form, exclusive) { var checkbox_id = \'shelf_name_\' + unique_id + \'_\' + shelf_id; var element = document.getElementById(checkbox_id) var checked = element.checked if (checked && exclusive) { // can\'t uncheck a radio by clicking it! return } if(document.getElementById("savingMessage")){ Element.show(\'savingMessage\') } var element_id = \'shelfInDropdownName_\' + unique_id + \'_\' + shelf_id; Element.upda

期望的输出:

“That does it," said Jace. "I\'m going to get you a dictionary for Christmas this year.""Why?" Isabelle said."So you can look up \'fun.\' I\'m not sure you know what it means.” ― Cassandra Clare

最佳答案

这是一种解决方案:

import re

s = 'adflakjd, fkljlkjdf ― Cassandra Clare, City of Ash, adflak'

x = re.findall('.*―.*?(?=,)', s)


print x

['adflakjd, fkljlkjdf ― Cassandra Clare']

关于python - 正则表达式/Python : Find everything before one symbol, 如果它在另一个符号之后,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48447614/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com