任何人都可以解释这个代码有什么问题吗?
str1='"xxx"'
print str1
if str1[:1].startswith('"'):
if str1[:-1].endswith('"'):
print "hi"
else:
print "condition fails"
else:
print "bye"
Run Code Online (Sandbox Code Playgroud)
我得到的输出是:
Condition fails
Run Code Online (Sandbox Code Playgroud)
但我希望它能打印出来hi.
我只能在我的程序中使用一个字符串,如果它不包含除下划线之外的特殊字符_.我怎么检查这个?
我尝试使用unicodedata库.但是特殊字符刚被标准字符所取代.
我wana检查我的字符串是否以花括号开头{.我尝试了以下代码.
class parser(object):
def __init__(self):
self.fp=open('jsondata.txt','r')
self.str=self.fp.read()
print "Name of the file Opened is :",self.fp.name
print "Contents of the file :\n",self.str
def rule1(self):
var='{'
if self.str[:0]==var:
print "good match"
else:
print "No match"
obj=parser()
obj.rule1()
Run Code Online (Sandbox Code Playgroud)
该文件包含:{"name":"Chuvi"}
但我的输出是:不匹配
我甚至试过以下但是得到了输出
if self.str[:0]=='{':
print "good match"
else:
print "No match"
Run Code Online (Sandbox Code Playgroud)