每小时将字符串转换为日期时间和时区
String convertion to Datetime and zone hourly
我的问题是通过 html 解析器站点,我在格式字符串中提取时间。
我需要将字符串转换为日期时间,然后转换为您的时区。
谢谢
hora = soup.select('span.match-time')[0].get_text().strip( ' - ' )
dt_obj = datetime.strptime(hora, '%H:%M')
print (dt_obj)
input_time = datetime.strptime(dt_obj, '%H:%M').time()
utc_dt = datetime.combine(datetime.utcnow(), input_time)
tz = pytz.timezone('America/Montevideo')
salida = str(utc_dt.replace(tzinfo=pytz.utc).astimezone(tz))
错误
/
System/Library/Frameworks/Python.framework/Versions/2.6/bin/python2.6 /Users/fa/Desarrollo/football/parser.py
1900-01-01 19:30:00
Traceback (most recent call last):
File "/Users/fa/Desarrollo/football/parser.py", line 24, in <module>
input_time = datetime.strptime(dt_obj, '%H:%M').time()
TypeError: strptime() argument 1 must be string, not datetime.datetime
Process finished with exit code 1
datetime.datetime.strptime 接受一个你想从中解析出时间的字符串,接受一种你指定如何将时间写入字符串的格式,然后它解析字符串以提取出一个日期时间对象。
再读一遍以确保您真正理解它。它产生错误的原因现在应该很明显了!您正在将对象传递给需要两个字符串的函数。
一旦将其解析为对象,就可以对其调用方法,如下所示:
>>> dt_obj = datetime.datetime.strptime("12:00", "%H:%M")
>>> dt_obj
datetime.datetime(1900, 1, 1, 12, 0)
>>> dt_obj.hour
12
>>> dt_obj.minute
0
>>> dt_obj.time()
datetime.time(12, 0)
>>> dt_obj.year
1900
编辑 1:
据我所知,您提取了小时和分钟,然后按照 UTC 标准使用今天的 date/month/year 构建日期时间对象。不过我不太明白你对时区做了什么,所以我会把最后两行留给你。
>>> dt_obj = datetime.strptime("12:00", "%H:%M")
>>> input_time = dt_obj.time()
>>> utc_dt = datetime.combine(datetime.utcnow(), input_time)
或者如果您需要更短的时间
>>> dt_obj = datetime.strptime("12:00", "%H:%M")
>>> utc_dt = datetime.combine(datetime.utcnow(), dt_obj.time())
这让你从 12:00
到 datetime.datetime(2015, 2, 11, 12, 0)
如果输入的时间字符串如19:30
指的是UTC时间:
#!/usr/bin/env python
from datetime import datetime
import pytz
tz = pytz.timezone('America/Montevideo')
time_string = '19:30'
utc_dt = datetime.combine(datetime.utcnow(),
datetime.strptime(time_string, '%H:%M').time())
dt = utc_dt.replace(tzinfo=pytz.utc).astimezone(tz)
print(dt.strftime('%Y-%m-%d %H:%M:%S %Z%z'))
# -> 2015-02-11 17:30:00 UYST-0200
如果输入的时间是乌拉圭时间:
#!/usr/bin/env python
from datetime import datetime
import pytz
tz = pytz.timezone('America/Montevideo')
time_string = '19:30'
naive_dt = datetime.combine(datetime.now(tz),
datetime.strptime(time_string, '%H:%M').time())
dt = tz.localize(naive_dt, is_dst=None)
print(dt.strftime('%Y-%m-%d %H:%M:%S %Z%z'))
# -> 2015-02-11 19:30:00 UYST-0200
注意:结果不同
我的问题是通过 html 解析器站点,我在格式字符串中提取时间。 我需要将字符串转换为日期时间,然后转换为您的时区。
谢谢
hora = soup.select('span.match-time')[0].get_text().strip( ' - ' )
dt_obj = datetime.strptime(hora, '%H:%M')
print (dt_obj)
input_time = datetime.strptime(dt_obj, '%H:%M').time()
utc_dt = datetime.combine(datetime.utcnow(), input_time)
tz = pytz.timezone('America/Montevideo')
salida = str(utc_dt.replace(tzinfo=pytz.utc).astimezone(tz))
错误 /
System/Library/Frameworks/Python.framework/Versions/2.6/bin/python2.6 /Users/fa/Desarrollo/football/parser.py
1900-01-01 19:30:00
Traceback (most recent call last):
File "/Users/fa/Desarrollo/football/parser.py", line 24, in <module>
input_time = datetime.strptime(dt_obj, '%H:%M').time()
TypeError: strptime() argument 1 must be string, not datetime.datetime
Process finished with exit code 1
datetime.datetime.strptime 接受一个你想从中解析出时间的字符串,接受一种你指定如何将时间写入字符串的格式,然后它解析字符串以提取出一个日期时间对象。
再读一遍以确保您真正理解它。它产生错误的原因现在应该很明显了!您正在将对象传递给需要两个字符串的函数。
一旦将其解析为对象,就可以对其调用方法,如下所示:
>>> dt_obj = datetime.datetime.strptime("12:00", "%H:%M")
>>> dt_obj
datetime.datetime(1900, 1, 1, 12, 0)
>>> dt_obj.hour
12
>>> dt_obj.minute
0
>>> dt_obj.time()
datetime.time(12, 0)
>>> dt_obj.year
1900
编辑 1:
据我所知,您提取了小时和分钟,然后按照 UTC 标准使用今天的 date/month/year 构建日期时间对象。不过我不太明白你对时区做了什么,所以我会把最后两行留给你。
>>> dt_obj = datetime.strptime("12:00", "%H:%M")
>>> input_time = dt_obj.time()
>>> utc_dt = datetime.combine(datetime.utcnow(), input_time)
或者如果您需要更短的时间
>>> dt_obj = datetime.strptime("12:00", "%H:%M")
>>> utc_dt = datetime.combine(datetime.utcnow(), dt_obj.time())
这让你从 12:00
到 datetime.datetime(2015, 2, 11, 12, 0)
如果输入的时间字符串如19:30
指的是UTC时间:
#!/usr/bin/env python
from datetime import datetime
import pytz
tz = pytz.timezone('America/Montevideo')
time_string = '19:30'
utc_dt = datetime.combine(datetime.utcnow(),
datetime.strptime(time_string, '%H:%M').time())
dt = utc_dt.replace(tzinfo=pytz.utc).astimezone(tz)
print(dt.strftime('%Y-%m-%d %H:%M:%S %Z%z'))
# -> 2015-02-11 17:30:00 UYST-0200
如果输入的时间是乌拉圭时间:
#!/usr/bin/env python
from datetime import datetime
import pytz
tz = pytz.timezone('America/Montevideo')
time_string = '19:30'
naive_dt = datetime.combine(datetime.now(tz),
datetime.strptime(time_string, '%H:%M').time())
dt = tz.localize(naive_dt, is_dst=None)
print(dt.strftime('%Y-%m-%d %H:%M:%S %Z%z'))
# -> 2015-02-11 19:30:00 UYST-0200
注意:结果不同