首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >为什么在python中类型(‘string’)似乎返回空字符串

为什么在python中类型(‘string’)似乎返回空字符串
EN

Stack Overflow用户
提问于 2012-08-06 02:38:17
回答 1查看 360关注 0票数 0

我正在阅读Mark的编程Python3编辑,我对一个问题感到困惑:type('something')总是导致一个空字符串。

有人能解释一下吗?

上下文信息:

我在脚本$CODEROOT\pp3e\Internet\Web\cgi-bin\tutor0.py中添加一行

代码语言:javascript
复制
#!/usr/bin/python
#######################################################
# runs on the server, prints html to create a new page;
# url=http://localhost/cgi-bin/tutor0.py
#######################################################

print "Content-type: text/html\n"
print "<TITLE>CGI 101</TITLE>"
print "<H1>A First CGI script</H1>"
print '<p>[%s]'%type('apple') # ★ I add this line
print "<P>Hello, CGI World!</P>"

对于添加的行,我希望在browser [<type 'str'>] 中看到,但实际上我看到的是 [] .

启动HTTP服务器的python位于$CODEROOT\pp3e\Internet\Web\webserver.py

代码语言:javascript
复制
#########################################################################
# implement HTTP web server in Python which knows how to serve HTML
# pages and run server side CGI scripts;  serves files/scripts from
# the current working dir and port 80, unless command-line args;
# python scripts must be stored in webdir\cgi-bin or webdir\htbin;
# more than one of these may be running on the same machine to serve
# from different directories, as long as they listen on different ports;
#########################################################################

webdir = '.'   # where your html files and cgi-bin script directory live
port   = 80    # http://servername/ if 80, else use http://servername:xxxx/

import os, sys
from BaseHTTPServer import HTTPServer
from CGIHTTPServer  import CGIHTTPRequestHandler

if len(sys.argv) > 1: webdir = sys.argv[1]             # command-line args
if len(sys.argv) > 2: port   = int(sys.argv[2])        # else dafault ., 80
print 'webdir "%s", port %s' % (webdir, port)

# hack for Windows: os.environ not propogated
# to subprocess by os.popen2, force in-process
if sys.platform[:3] == 'win':
    CGIHTTPRequestHandler.have_popen2 = False
    CGIHTTPRequestHandler.have_popen3 = False          # emulate path after fork
    sys.path.append('cgi-bin')                         # else only adds my dir

os.chdir(webdir)                                       # run in html root dir
srvraddr = ("", port)                                  # my hostname, portnumber
srvrobj  = HTTPServer(srvraddr, CGIHTTPRequestHandler)
srvrobj.serve_forever()                                # serve clients till exit

我的环境:

  • Python 2.7
  • Windows 7 x64
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2012-08-06 02:42:55

可能是因为<type 'str'>中的尖括号导致输出被视为HTML。

试着改变:

代码语言:javascript
复制
print '<p>[%s]'%type('apple') # ★ I add this line

转入:

代码语言:javascript
复制
hstr = "%s"%type('apple')
hstr = hstr.replace('<','&lt;').replace('>','&gt;')
print '<p>[%s]'%hstr
票数 6
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/11821930

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档