【发布时间】:2016-05-24 17:55:17
【问题描述】:
希望了解 Python 的tokenize 模块。我有兴趣在给定的 python 源文件(如下面的)上调用 tokenize.tokenize 方法,并使用文档中提到的 5 元组获取其标记化输出。
# Python source file
import os
class Test():
"""
This class holds latitude, longitude, depth and magnitude data.
"""
def __init__(self, latitude, longitude, depth, magnitude):
self.latitude = latitude
self.longitude = longitude
self.depth = depth
self.magnitude = magnitude
def __str__(self):
# -1 is for detection of missing data
depth = self.depth
if depth == -1:
depth = 'unknown'
magnitude = self.magnitude
if magnitude == -1:
depth = 'unknown'
return "M{0}, {1} km, lat {2}\N{DEGREE SIGN} lon {3}\N{DEGREE SIGN}".format(magnitude, depth, self.latitude, self.longitude)
不幸的是,文档中的example 不够清楚,因为我对 Python 缺乏经验,无法使其正常工作。另外,我在网上找不到任何相关的有用示例代码。
任何简单可行的代码示例将不胜感激。
此外,如果您知道有用的在线材料以及 tokenize 模块及其方法的示例/说明,那就太好了。
【问题讨论】:
标签: python python-3.x tokenize