gpt4 book ai didi

python - 使用Python 3删除一些输出内容

转载 作者:太空宇宙 更新时间:2023-11-03 14:07:47 24 4
gpt4 key购买 nike

我得到的输出如下:

Review: This hotel has been here awhile. However, they’ve kept it up nicely. The staff is very professional and friendly. The rooms have everything you need. Only con, the ice machines are on the 2nd and 8th floors only. Nice place, great location.
0
0 None
Review: Thank you for taking a moment to share your experience. I am pleased to hear you found the hotel and staff to your liking. We look forward to welcoming you back to our hotel in the not too distant future.Sincerely,Andrea McLeodAssistant Front Office ManagerHilton New York Grand CentralAndrea.McLeod@Hilton.com 212-986-8800
0
0 None
Review: I have wasted lot of time looking for a laundry service as there is none in the hotel, the ones they have leaves at saturdays 9:30 am, and then you don't have any other option (neither assistance from the desk). The shower doors open alone, so everything gets wet and there is no place to put your soap or shampoo... This is like a 2 stars hotel :S I hope my company can book me another one for my following stays.
0
0 None
Review: Dear Manuel AThank you for having chosen our hotel for your trip to New York. We apologize that the door did not close properly. If you do return to the hotel please let us know if any issues that you may have and we will be more than happy to fix them. Thank you again for choosing the Hilton Grand Central.
0

我想删除从开始的第二段和第四段评论:感谢您花点时间分享您的经验。我很高兴......评论:亲爱的曼努埃尔A感谢您选择我们的酒店作为您的纽约之旅。 ..

如何使用 python3 从输出中删除这两段?

这是代码的更新版本,工作正常。但是如何使用 CSV 格式的 panadas 将输出保存为数据帧格式?

for dtags in html.find_all('div', attrs={'class':'wrap'}):
for index, ptags in enumerate(dtags.find_all('p', attrs={'class':'partial_entry'})):
if index == 0: #match the first element
x = ptags.text
z = print('Review:', x)

最佳答案

看来您正在从 tripadvisor 解析此页面.

与其解析输出,不如更准确地选择 <p>页面中的元素。这些评论中的每一条(以及所有者的回复)都属于一个名为 wrap 的类。 ,所以我们可以找到所有这些 div,然后找到 partial_entry 的第一个匹配项类,而不是在选择所有内容后试图弄清楚我们是在查看回复还是评论。

for dtags in html.find_all('div', attrs={'class':'wrap'}):
for index, ptags in enumerate(dtags.find_all('p', attrs={'class':'partial_entry'})):
if index == 0: #match the first element
x = ptags.text
z = print('Review:', x)

关于python - 使用Python 3删除一些输出内容,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48742452/

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