python - Python 中的时间比较

标签 python python-2.7

我当前的项目是从网站上抓取天气数据进行计算。根据当前时间是在中午之前还是中午之后,此计算的一部分涉及不同的逻辑。

import pandas as pd
from bs4 import BeautifulSoup
import requests
import numpy as np  

# Arkansas State Plant Board Weather Web data
url1 = "http://170.94.200.136/weather/Inversion.aspx"
response1 = requests.get(url1)

soup1 = BeautifulSoup(response1.content)
table1 = soup1.find("table", id="MainContent_GridView1")

data1 = pd.read_html(str(table1),header=0)[0] 
data1.columns = ['Station', 'Low Temp (F)', 'Time of Low', 'Current Temp (F)', 'Current Time',  'Wind Speed (MPH)', 'Wind Dir', 'High Temp (F)', 'Time Of High']

print(url1)
print(data1[0:4])

array1 = np.array(data1[0:4])

这是我的代码,用于引入我需要的数据。但是,我不知道如何将我请求的当前时间作为 Unicode 字符串进行比较,以查看它是在中午之前还是之后。谁能帮我解决这个问题吗?

编辑:当前请求的一些数据

    Station  Low Temp (F) Time of Low  Current Temp (F) Current Time  \
0  Arkansas          69.0     5:19 AM              88.7      2:09 PM   
1    Ashley          70.4     4:39 AM              91.2      2:14 PM   
2   Bradley          69.4     4:09 AM              90.6      2:14 PM   
3    Chicot         -40.2     2:14 PM             -40.2      2:14 PM   

   Wind Speed (MPH)  Wind Dir  High Temp (F) Time Of High  
0               4.3       213           88.9      2:04 PM  
1               4.1       172           91.2      2:14 PM  
2               6.0       203           90.6      2:09 PM  
3               2.2       201          -40.1     12:24 AM  

最佳答案

只需检查子午线是下午还是上午。

 time = "2:09 PM"
 meridian = time.split(' ')[-1] # get just the meridian
 before_noon = meridian == 'AM'
 after_noon = meridian == 'PM'

关于python - Python 中的时间比较,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38314322/

相关文章:

python-2.7 - Python “expected an indented block”

python - 查找单词在字符串中的位置

python - 通过索引访问行号

python - Django - 模型外观

python - 单行基于两个字典计算分数

python - 使用 DJANGO 框架进行 INNER JOIN

pandas - 将数据写入 Excel 给我 'ZIP does not support timestamps before 1980'

python-2.7 - 我可以使用树莓派(型号 B+)的 USB 端口进行串行通信(RS232)吗?

python - 从一系列 Pandas 时间戳中提取月份的最快方法

java - Elasticsearch Java 中的 MultiTermVectors