tzwhere (pytzwhere) - Python 2.7 的简单测试案例

1 投票
1 回答
1122 浏览
提问于 2025-04-18 07:08

我正在运行一个非常简单的测试程序,这个程序是用pytzwhere这个包提供的。这个模块可以正常导入,但我遇到了一个错误。pytzwhere看起来是一个不错的离线选项,可以根据GPS坐标获取时区,但它的文档不太多。希望能得到一些帮助,解决这个问题!

In [94]:

import tzwhere

w = tzwhere()
print w.tzNameAt(1.352083, 103.819836)
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-94-0b50c8083e93> in <module>()
      1 import tzwhere
      2 
----> 3 w = tzwhere()
      4 print w.tzNameAt(1.352083, 103.819836)

TypeError: 'module' object is not callable

编辑:

根据下面的评论,我已经通过以下代码修改解决了这个问题 -

In [108]:

from tzwhere import tzwhere

w = tzwhere.tzwhere()
print w.tzNameAt(1.352083, 103.819836)
-----------------------------------------------------------------------------
Reading json input file: /Users/user/anaconda/lib/python2.7/site-packages/tzwhere/tz_world_compact.json
Asia/Singapore

1 个回答

1

你不能直接用像 w = tzwhere() 这样的方式来创建 w。tzwhere 是一个模块,里面包含了一个叫做 tzwhere 的类。正如 Python 所说的,模块是不能直接调用的。

from tzwhere import tzwhere

第一行代码是从 模块 tzwhere 中导入 tzwhere。

补充一下,如果你用我说的方式导入 :-) w = tzwhere() 也是可以的,这样可以正确创建 w 作为 tzwhere 的一个实例。

通常在 Python 中,类的命名会用大写字母,比如 TzWhere,这样可以避免这种混淆。

我猜你是想使用这个链接中的内容:https://github.com/pegler/pytzwhere/blob/master/tzwhere/tzwhere.py

撰写回答