Python CGI调试的2两种方法
- #linuxany_cgi.py
- import sys
- sys.stderr = sys.stdout
- def main():
- import cgi
- # ...do the actual work of the CGI...
- # perhaps ending with:
- print template % script_dictionary
- print "Content-type: text/html\n\n"
- main()
Formatting sprintf()-style in Python
在将各种类型的数据构造成字符串时,sprintf 的强大功能很少会让你失望。由于sprintf 跟printf 在用法上几乎一样,只是打印的目的地不同而已,前者打印到字符串中,后者则直 接在命令行上输出。这也导致sprintf 比printf 有用得多。
sprintf 是个变参函数,定义如下:
除了前两个参数类型固定外,后面可以接任意多个参数。而它的精华,显然就在第二个参数:
格式化字符串上。
printf 和sprintf 都使用格式化字符串来指定串的格式,在格式串内部使用一些以“%”开头的格式说明符(format specifications)来占据一个位置,在后边的变参列表中提供相应的变量,最终函数就会用相应位置的变量来替代那个说明符,产生一个调用者想要的字符串。