python的keyword模块用法实例分析

yipeiwu_com5年前Python基础

本文实例讲述了python的keyword模块用法。分享给大家供大家参考。具体如下:

Help on module keyword:
NAME
  keyword - Keywords (from "graminit.c")
FILE
  /usr/lib64/python2.6/keyword.py
DESCRIPTION
  This file is automatically generated; please don't muck it up!
  To update the symbols in this file, 'cd' to the top directory of
  the python source tree after building the interpreter and run:
    python Lib/keyword.py
FUNCTIONS
  iskeyword = __contains__(...)
    x.__contains__(y) <==> y in x.
DATA
  __all__ = ['iskeyword', 'kwlist']
  kwlist = ['and', 'as', 'assert', 'break', 'class', 'continue', 'def', ...

得到python的关键字列表:

>>> keyword.kwlist
['and', 'as', 'assert', 'break', 'class', 'continue', 'def', 'del', 'elif', 'else', 'except', 'exec', 'finally', 'for', 'from', 'global', 'if', 'import', 'in', 'is', 'lambda', 'not', 'or', 'pass', 'print', 'raise', 'return', 'try', 'while', 'with', 'yield']

判断字符串是否是python的关键字

>>> keyword.iskeyword('and')
True
>>> 
>>> keyword.iskeyword('has')
False

希望本文所述对大家的Python程序设计有所帮助。

相关文章

python分块读取大数据,避免内存不足的方法

如下所示: def read_data(file_name): ''' file_name:文件地址 ''' inputfile = open(file_name, 'rb'...

浅谈python下tiff图像的读取和保存方法

对比测试 scipy.misc 和 PIL.Image 和 libtiff.TIFF 三个库 输入: 1. (读取矩阵) 读入uint8、uint16、float32的lena.tif...

python中将正则过滤的内容输出写入到文件中的实例

处理过滤Apache日志文件 access_test.log文件内容 27.19.74.143 - - [30/May/2015:17:38:21 +0800] "GET /stat...

深入解析Python中的变量和赋值运算符

深入解析Python中的变量和赋值运算符

Python 变量类型 变量存储在内存中的值。这就意味着在创建变量时会在内存中开辟一个空间。 基于变量的数据类型,解释器会分配指定内存,并决定什么数据可以被存储在内存中。 因此,变量可以...

Python字符串格式化的方法(两种)

本文介绍了Python字符串格式化,主要有两种方法,分享给大家,具体如下 用于字符串的拼接,性能更优。 字符串格式化有两种方式:百分号方式、format方式。 百分号方式比较老,而for...