我想检查我的字符串是否以花括号 {. 我尝试了以下代码。
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()
该文件包含: {"name":"Chuvi"}
但我的输出是:不匹配
我什至尝试了以下但力求输出
if self.str[:0]=='{':
print "good match"
else:
print "No match"