使用pytz python包将UTC时间转换为CST时间

使用pytz python包将UTC时间转换为CST时间,python,python-3.x,datetime,pytz,Python,Python 3.x,Datetime,Pytz,我有一个嵌套的json文件,它有UTC格式的时区,我正在捕获它并将其放入一列中,然后尝试通过为cst创建一列来将其转换为cst,但它没有转换。有人能帮我发布下面的代码吗 def extract_json_data(fpath): print("Extracting " + fpath) f = open(fpath, 'r') json_data = json.loads(f.read()) data = json_data['data'] dt = da

我有一个嵌套的json文件,它有UTC格式的时区,我正在捕获它并将其放入一列中,然后尝试通过为cst创建一列来将其转换为cst,但它没有转换。有人能帮我发布下面的代码吗

def extract_json_data(fpath):
    print("Extracting " + fpath)
    f = open(fpath, 'r')
    json_data = json.loads(f.read())
    data = json_data['data']
    dt = datetime.datetime.strptime(json_data['time'], "%Y-%m-%dT%H:%M:%SZ")
    dt_cst = dt.astimezone(timezone('US/Central'))
    _ = [row.update({'time_UTC': dt.strftime("%Y-%m-%dT%H:%M:%SZ"),
                     'time_CST': dt_cst.strftime("%Y-%m-%dT%H:%M:%S CST")}) for row in data]

这里有一种方法:

import datetime
from dateutil import tz

# create a time-zone object representing UTC. 
from_zone = tz.gettz('UTC')

# Create another time zone object, representing the target time zone. 
# note that according to the tz package documentation 
# (https://dateutil.readthedocs.io/en/stable/tz.html#dateutil.tz.gettz), 
# they have Windows-specific time-zone names support. 
to_zone = tz.gettz('America/Chicago')

# This is just a sample dictionary, so I cam extract the 'time' 
# field like you do in your code. It's really not needed here. 
json_data = {'time': "2020-05-16T08:17:42Z"} # an example for a datetime

# Create a datetime object, representing the UTC time. 
utc = datetime.datetime.strptime(json_data['time'], "%Y-%m-%dT%H:%M:%SZ")

# now replace the timezone field of the newly created datetime object, 
# so it would be UTC. 
utc = utc.replace(tzinfo=from_zone)

# Convert time zone from UTC to central
central = utc.astimezone(to_zone)

print(central)
您将获得:


2020-05-16 03:17:42-05:00

使用格式字符串解析时区,以便使用的datetime对象能够识别时区:

from datetime import datetime

# the string actually has timezone information: Z (UTC)
timestring = "2019-01-01T00:00:00Z"

# wrong:
dt = datetime.strptime(timestring, "%Y-%m-%dT%H:%M:%SZ")
# dt is naive:
# datetime.datetime(2019, 1, 1, 0, 0)

# right:
dt = datetime.strptime(timestring, "%Y-%m-%dT%H:%M:%S%z")
# dt now knows it's in UTC:
# datetime.datetime(2019, 1, 1, 0, 0, tzinfo=datetime.timezone.utc)
现在,您可以将datetime对象的时间更改为其他时区:

import pytz
tz = pytz.timezone('US/Central')
dt_cst = dt.astimezone(tz)
# datetime.datetime(2018, 12, 31, 18, 0, tzinfo=<DstTzInfo 'US/Central' CST-1 day, 18:00:00 STD>)

您好Roy您是否可以在我尝试时逐行解释您的代码,但不起作用我可能遗漏了一些东西我会解释我的,如果您能指出我的错误,我将读取JSON文件,然后从中提取时间并放入列中,然后尝试将列时间转换为cst并存储它在另一个coloum@user13073332. 添加了一些注释。你说它不工作-如果按原样运行我的代码会发生什么?你的示例时间字符串“2020-05-16T08:17:42Z”的末尾有一个时区,那么为什么要使用
replace()
?只需为
strptime
使用正确的格式字符串即可。如果您拼写正确并将其包含在代码示例中,您可能会在这个问题上获得更好的帮助。您的格式字符串
%Y-%m-%dT%H:%m:%SZ“
最后解析一个文本
Z
,我认为您想要解析时区(Z;UTC),因此,请使用
%Y-%m-%dT%H:%m:%S%z”
-请参阅下面我的答案。
import dateutil
timestring = "2019-01-01T00:00:00Z"
dt = dateutil.parser.parse(timestring)

# dt
# datetime.datetime(2019, 1, 1, 0, 0, tzinfo=tzutc())