我正在使用:
- Python 3.11.1
- Windows 10 Pro
- 请求2.31.0
- 美丽的汤4.12.2
- pandas 2.1.2
- jupyter(用jup编写,但我会用PyCharm完成代码)
我通过html请求从学院网站上获得文本,他们在那里发布了课程表,我得到了它们,但在分散的顺序,你可以看到在图像和文本文件(下面的链接),我不能写正则表达式,使文本可读,帮助我解决这个问题
from bs4 import BeautifulSoup
import re
import requests
import pandas as pd
url = 'https://docs.google.com/spreadsheets/d/e/2PACX-1vSNR-Gvp7MBcYQo0GM5nU3UC7DSIGMCwKq-eQGIY_alqORpe1pvZ00PI63wNuOyiJbZI_AP6nSeWWop/pubhtml'
page = requests.get(url)
soup = BeautifulSoup(page.text, 'html')
table = soup.find_all('table')[0]
world_titles = table.find_all('td')
world_table_titles = [title.text.strip() for title in world_titles]
# here I can't write reg exp to make text readable
clean_titles = [re.sub(r'[",\s]+', '', title) for title in world_table_titles]
print(clean_titles)
字符串
我会很高兴地得到关于如何使文本可读类型的指示:
ПОНЕДЕЛЬНИК
Время АСОИ-1-23
8:00
Физика (пр) Нарманбетова Г. Ж
9:30 Математика Алыкулова К.Б
11:00 Основы экономики, менеджмента и маркетинга Алтыбаева Ш.И.
12:40 Русский язык Омуркулова Г.М.
型
我知道要求很多,但我真的被困住了
text fileregular_expression的数据库
我在看youtube教程,regex101,人工智能聊天机器人,但任何东西都没有帮助
1条答案
按热度按时间wmomyfyw1#
我想这是一个XY problem。
假设你想加载一个Google表格(从它的
pubhtml
url)来查询特定的数据,也许你应该考虑使用pandas的read_html
,并进行一些 * 后处理 *?字符串
这就形成了一个层次结构的嵌套框架,
loc
将给予预期的输出:型
输出(* 整个表 *):
型
为了好玩,如果你也想克隆格式,你可以使用Styler:
型
的数据