标签类目:sprintf

Python CGI调试的2两种方法

分类:Python第1种:直接输出标准错误

  1. #linuxany_cgi.py
  2. import sys
  3. sys.stderr = sys.stdout
  4.  
  5. def main():
  6.     import cgi
  7.     # ...do the actual work of the CGI...
  8.     # perhaps ending with:
  9.     print template % script_dictionary
  10.  
  11. print "Content-type: text/html\n\n"
  12. main()

继续阅读 »

Python CGI中两种风格的字符串格式化

分类:PythonFormatting sprintf()-style in Python

  1. print """<html><head>
  2. <title>%s</title>
  3. </head><body>
  4. <h1>Famous irrational numbers</h1>
  5. <dl><dt>Pi</dt>
  6.     <dd>%2.3f</dd>
  7.     <dt>Square-root of 2</dt>
  8.     <dd>%2.3f</dd></dl>
  9. </body></html>""" % ("linuxany.com-website", 3.1415, 1.4142)

继续阅读 »

sprintf()的一些高级用法

分类:C/C++在将各种类型的数据构造成字符串时,sprintf 的强大功能很少会让你失望。由于sprintf 跟printf 在用法上几乎一样,只是打印的目的地不同而已,前者打印到字符串中,后者则直 接在命令行上输出。这也导致sprintf 比printf 有用得多。

sprintf 是个变参函数,定义如下:

  1. int sprintf( char *buffer, const char *format [, argument] ... );

除了前两个参数类型固定外,后面可以接任意多个参数。而它的精华,显然就在第二个参数:
格式化字符串上。

printf 和sprintf 都使用格式化字符串来指定串的格式,在格式串内部使用一些以“%”开头的格式说明符(format specifications)来占据一个位置,在后边的变参列表中提供相应的变量,最终函数就会用相应位置的变量来替代那个说明符,产生一个调用者想要的字符串。

继续阅读 »


返回顶部