简体   繁体   English

TypeError:replace()在更改时区时不使用关键字参数

[英]TypeError: replace() takes no keyword arguments on changing timezone

I'm trying to change the timezone of UTC to America/Sao Paulo, but I'm getting this error: 我正在尝试将UTC的时区更改为America / Sao Paulo,但我收到此错误:

TypeError: replace() takes no keyword arguments

This is my code: 这是我的代码:

import pytz

local_tz = pytz.timezone('America/Sao_Paulo')
local_dt = candles[0]['time'].replace(tzinfo=pytz.utc).astimezone(local_tz)

Candle time is: 蜡烛时间是:

>>> candles[0]['time']
u'2017-08-03T00:03:00.000000Z'

How can I fix this? 我怎样才能解决这个问题?

You need to convert your candles[0]['time'] which is a unicode string to datetime object. 您需要将您的candles[0]['time']这是一个unicode字符串)转换为datetime对象。

Here is an example: 这是一个例子:

import datetime, pytz

a = u'2017-08-03T00:03:00.000000Z'
local_tz = pytz.timezone('America/Sao_Paulo')
local_dt = datetime.datetime.strptime(a, '%Y-%m-%dT%H:%M:%S.000000Z').replace(tzinfo=pytz.utc).astimezone(local_tz)
print local_dt

Output: 输出:

2017-08-02 21:03:00-03:00

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM