第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

如何在不知道位置的情況下在某個點拆分字符串。

如何在不知道位置的情況下在某個點拆分字符串。

寶慕林4294392 2022-01-18 17:23:37
我目前正在從 TFL API 中提取天氣預(yù)報。一旦為“今天的預(yù)測”提取了 json,段落中間就會出現(xiàn)隨機符號——我認為這可能是從 API 格式化的。這是提取的內(nèi)容:Bank holiday Monday will stay dry with some long sunny spells. Temperatures will remain warm for the time of year.<br/><br/>PM2.5 particle pollution increased rapidly overnight. Increases began across Essex and spread across south London.  Initial chemical analysis suggests that this is composed mainly of wood burning particles but also with some additional particle pollution from agriculture and traffic. This would be consistent with an air flow from the continent where large bonfires are part of the Easter tradition. This will combine with our local emissions today and 'high' PM2.5 is possible.<br/><br/>The sunny periods, high temperatures and east winds will bring additional ozone precursors allowing for photo-chemical generation of ozone to take place. Therefore 'moderate' ozone is likely.<br/><br/>Air pollution should remain 'Low' through the forecast period for the following pollutants:<br/><br/>Nitrogen Dioxide<br/>Sulphur Dioxide.這一段比必要的更詳細,前兩句話就是我所需要的。我認為.split這是一個好主意,并通過 for 循環(huán)運行它,直到它到達 string "<br/><br/>PM2.5"。但是,我不能確定這是否每天都是相同的字符串,或者簡化的預(yù)測是否仍然只是前兩個句子。有人對我如何解決這個問題有任何想法嗎?作為參考,這是我目前擁有的代碼,它還不是其他任何東西的一部分。import urllib.parseimport requestsmain_api = "https://api.tfl.gov.uk/AirQuality?"idno = "1"url = main_api + urllib.parse.urlencode({"$id": idno})json_data = requests.get(main_api).json()disclaimer = json_data['disclaimerText']print("Disclaimer: " + disclaimer)print()today_weather = json_data['currentForecast'][0]['forecastText']print("Today's forecast: " + today_weather.replace("<br/><br/>"," "))
查看完整描述

3 回答

?
陪伴而非守候

TA貢獻1757條經(jīng)驗 獲得超8個贊

我相信,如果您清理 HTML 標記,然后使用 NLTK 的句子標記器對段落進行標記,那么您應(yīng)該很高興。


from nltk.tokenize import sent_tokenize


import urllib.parse

import requests

import re


main_api = "https://api.tfl.gov.uk/AirQuality?"


idno = "1"

url = main_api + urllib.parse.urlencode({"$id": idno})


json_data = requests.get(main_api).json()


disclaimer = json_data['disclaimerText']

print("Disclaimer: " + disclaimer)


print()


# Clean out HTML tags

today_weather_str = re.sub(r'<.*?>', '', json_data['currentForecast'][0]['forecastText'])


# Get the first two sentences out of the list

today_weather = ' '.join(sent_tokenize(today_weather_str)[:2])


print("Today's forecast: {}".format(today_weather))


查看完整回答
反對 回復(fù) 2022-01-18
?
慕俠2389804

TA貢獻1719條經(jīng)驗 獲得超6個贊

如果您要編寫一個沒有為每個數(shù)據(jù)集顯式編碼的腳本,那么您需要找到某種模式,如果該模式是您想要的字符串始終是前兩行,那么您可以使用for循環(huán):


data = [line for line in your_variable_here]


data = data[:2]

如果似乎有關(guān)于簡化預(yù)測的模式,您也可以嘗試使用正則表達式。


但是,如果沒有更多關(guān)于數(shù)據(jù)集是什么樣子的信息,我認為這是我能想到的最好的。


查看完整回答
反對 回復(fù) 2022-01-18
?
素胚勾勒不出你

TA貢獻1827條經(jīng)驗 獲得超9個贊

這些“隨機符號”

&lt;br/&gt;

是一個 HTML 編碼

<br/>

或 HTML 中的新行,因此看起來像是一個可靠的拆分方法:

lines = today_weather.split('&lt;br/&gt;')

我認為可以合理地假設(shè)第一行就是您所追求的:

short_forecast = lines[0]

時間會證明這是否正確,但您可以輕松調(diào)整以包含更多或更少。


查看完整回答
反對 回復(fù) 2022-01-18
  • 3 回答
  • 0 關(guān)注
  • 190 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動學(xué)習(xí)伙伴

公眾號

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號