本文整理汇总了Python中string.count函数的典型用法代码示例。如果您正苦于以下问题:Python count函数的具体用法?Python count怎么用?Python count使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了count函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: writeArray
def writeArray (xy, file=None, format='%g ', comments=[], commentChar='#'):
""" Write a numeric array xy to file (or stdout if unspecified).
(format must have one, two, or xy.shape[1] specifiers, line feed is appended if necessary.) """
if file:
if hasattr(file,'closed'):
if file.mode=='w': out=file
else: print 'ERROR --- IO.writeArray: file "' + file + '" opened in readmode!'
else:
out=open(file,'w')
# print command line as first line to output file
if not (sys.argv[0]=='' or 'ipython' in sys.argv[0]):
sys.argv[0] = os.path.basename(sys.argv[0])
out.write (commentChar + ' ' + join(sys.argv) + '\n' + commentChar + '\n')
else: out = sys.stdout
for com in comments: out.write ( '%s %s\n' % (commentChar, strip(com)) )
if len(xy.shape)==1:
if count(format,'\n')==0: format = format.rstrip()+'\n'
for i in range(xy.shape[0]): out.write (format % xy[i] )
elif len(xy.shape)==2:
npc = count(format,'%')
if npc==1:
format = xy.shape[1] * format
elif npc==2 and xy.shape[1]>2:
f2 = rfind(format,'%')
format = format[:f2] + (xy.shape[1]-1) * (' '+format[f2:])
elif npc!=xy.shape[1]:
print "ERROR --- IO.writeArray: check format (number of format specs does'nt match number of columns in data)"
return
if count(format,'\n')==0: format = format.rstrip()+'\n'
for i in range(xy.shape[0]): out.write (format % tuple(xy[i,:]) )
else:
print 'ERROR --- IO.writeArray: writing arrays with more than 2 dimensions not supported!'
if not out.closed: out.close()
示例2: deg2HMS
def deg2HMS(ra='', dec='', round=False):
import string
RA, DEC= '', ''
if dec:
if string.count(str(dec),':')==2:
dec00=string.split(dec,':')
dec0,dec1,dec2=float(dec00[0]),float(dec00[1]),float(dec00[2])
if '-' in str(dec0): DEC=(-1)*((dec2/60.+dec1)/60.+((-1)*dec0))
else: DEC=(dec2/60.+dec1)/60.+dec0
else:
if str(dec)[0]=='-': dec0=(-1)*abs(int(dec))
else: dec0=abs(int(dec))
dec1=int((abs(dec)-abs(dec0))*(60))
dec2=((((abs(dec))-abs(dec0))*60)-abs(dec1))*60
DEC='00'[len(str(dec0)):]+str(dec0)+':'+'00'[len(str(dec1)):]+str(dec1)+':'+'00'[len(str(int(dec2))):]+str(dec2)
if ra:
if string.count(str(ra),':')==2:
ra00=string.split(ra,':')
ra0,ra1,ra2=float(ra00[0]),float(ra00[1]),float(ra00[2])
RA=((ra2/60.+ra1)/60.+ra0)*15.
else:
ra0=int(ra/15.)
ra1=int(((ra/15.)-ra0)*(60))
ra2=((((ra/15.)-ra0)*60)-ra1)*60
RA='00'[len(str(ra0)):]+str(ra0)+':'+'00'[len(str(ra1)):]+str(ra1)+':'+'00'[len(str(int(ra2))):]+str(ra2)
if ra and dec: return RA, DEC
else: return RA or DEC
示例3: bitrigramFeature
def bitrigramFeature(self):
bi_template=[]
tri_template=[]
biFeature={}
triFeature={}
for l in string.lowercase:
for m in string.lowercase:
for n in string.lowercase:
tri_template.append(l+m+n)
bi_template.append(l+m)
words=''
for temp in self.sentences:
if self.output==False:
length=len(temp)-3
else:
length=len(temp)-2
for i in range(length):
words +=temp[i]+' '
for elem in bi_template:
biFeature[elem]=float(string.count(words,elem))
for elem in tri_template:
triFeature[elem]=float(string.count(words,elem))
triSum=sum(triFeature.values())
biSum=sum(biFeature.values())
for elem in biFeature:
biFeature[elem]=biFeature[elem]/biSum
for elem in triFeature:
triFeature[elem]=triFeature[elem]/triSum
return biFeature, triFeature
示例4: scoreParse
def scoreParse(self, parse, correct):
"""Compares the algorithms parse and the correct parse and assigns a score for that parse.
The score is based upon how far apart the right and left parentheses are from each other
in the two parses and also includes a penalty for parentheses in one parse but not the
other.
"""
score = 0
compCnt = string.count(parse, "(")
realCnt = string.count(correct, "(")
compPos = -1
realPos = -1
for i in range(compCnt):
compPos = string.find(parse, "(", compPos+1, len(parse))
realPos = string.find(correct, "(", realPos+1, len(correct))
score += abs(compPos - realPos)
score += abs(compCnt - realCnt)
compCnt = string.count(parse, ")")
realCnt = string.count(correct, ")")
compPos = -1
realPos = -1
for i in range(compCnt):
compPos = string.find(parse, ")", compPos+1, len(parse))
realPos = string.find(correct, ")", realPos+1, len(correct))
score += abs(compPos - realPos)
score += abs(compCnt - realCnt)
return score
示例5: myFeatures
def myFeatures(string):
all_notes = re.findall(r"[0-9]0? *?/ *?10", string)
if all_notes:
print(all_notes)
all_notes = [int(x.split("/")[0].strip()) for x in all_notes]
mean = np.mean(all_notes)
maxim = np.max(all_notes)
minim = np.min(all_notes)
print(all_notes, mean, maxim, minim)
else:
mean = -1
maxim = -1
minim = -1
return [
len(string),
string.count("."),
string.count("!"),
string.count("?"),
len(re.findall(r"[^0-9a-zA-Z_ ]", string)), # Non aplha numeric
len(re.findall(r"10", string)),
len(re.findall(r"[0-9]", string)),
string.count("<"),
len(re.findall(r"star(s)?", string)),
mean,
maxim,
minim,
len(re.findall(r"[A-Z]", string)),
]
示例6: action
def action(self, user, cmd_list):
t = self.telnet
t.write("\n")
login_prompt = "login: "
response = t.read_until(login_prompt, 5)
if string.count(response, login_prompt):
print response
else:
return 0
password_prompt = "Password:"
t.write("%s\n" % user)
response = t.read_until(password_prompt, 3)
if string.count(response, password_prompt):
print response
else:
return 0
t.write("%s\n" % self.passwd[user])
response = t.read_until(self.command_prompt, 5)
if not string.count(response, self.command_prompt):
return 0
for cmd in cmd_list:
t.write("%s\n" % cmd)
response = t.read_until(self.command_prompt, self.timeout)
if not string.count(response, self.command_prompt):
return 0
print response
return 1
示例7: ScriptURL
def ScriptURL(target, web_page_url=None, absolute=0):
"""target - scriptname only, nothing extra
web_page_url - the list's configvar of the same name
absolute - a flag which if set, generates an absolute url
"""
if web_page_url is None:
web_page_url = mm_cfg.DEFAULT_URL
if web_page_url[-1] <> '/':
web_page_url = web_page_url + '/'
fullpath = os.environ.get('REQUEST_URI')
if fullpath is None:
fullpath = os.environ.get('SCRIPT_NAME', '') + \
os.environ.get('PATH_INFO', '')
baseurl = urlparse.urlparse(web_page_url)[2]
if not absolute and fullpath[:len(baseurl)] == baseurl:
# Use relative addressing
fullpath = fullpath[len(baseurl):]
i = string.find(fullpath, '?')
if i > 0:
count = string.count(fullpath, '/', 0, i)
else:
count = string.count(fullpath, '/')
path = ('../' * count) + target
else:
path = web_page_url + target
return path + mm_cfg.CGIEXT
示例8: fixcode
def fixcode(block, doctype):
# Some HTML preparation
block = Detag(block)
block = LeftMargin(block)
# Pull out title if available
re_title = re.compile('^#\-+ (.+) \-+#$', re.M)
if_title = re_title.match(block)
if if_title:
title = if_title.group(1)
block = re_title.sub('', block) # take title out of code
else: title = ''
#block = string.strip(block) # no surrounding whitespace
# Process the code block with Py2HTML (if possible and appropriate)
if py_formatter and (string.count(title,'.py') or
string.count(title,'Python') or
string.count(title,'python') or
string.count(title,'py_') or
doctype == 'PYTHON'):
fh = open('tmp', 'w')
fh.write(block)
fh.close()
py2html.main([None, '-format:rawhtml', 'tmp'])
block = open('tmp.html').read()
block = code_block % (title, block)
# elif the-will-and-the-way-is-there-to-format-language-X:
# elif the-will-and-the-way-is-there-to-format-language-Y:
else:
block = code_block % (title, '<pre>'+block+'</pre>')
return block
示例9: run
def run(self):
try:
if osflag:
proc=Popen(self.cmd,shell=False,stdin=None,stdout=PIPE,\
stderr=STDOUT,bufsize=0)
else:
from subprocess import STARTUPINFO
si=STARTUPINFO()
si.dwFlags|=1
si.wShowWindow=0
proc=Popen(self.cmd,shell=False,stdin=None,stdout=PIPE,\
stderr=STDOUT,bufsize=0,startupinfo=si)
while 1:
if self.stop_flag:
if osflag: proc.send_signal(signal.SIGKILL)
else: proc.kill()
break
if osflag:
if proc.stdout in select.select([proc.stdout],[],[],1)[0]:
line=proc.stdout.readline()
else: line=' \n'
else: line=proc.stdout.readline()
if not len(line): break
else:
if count(line,'ttl') or count(line,'TTL'): self.retries=0
else: self.retries=self.retries+1
line=' '
sleep(0.5)
proc.poll()
except: pass
示例10: main
def main():
pathname = EasyDialogs.AskFileForOpen(message="File to check end-of-lines in:")
if not pathname:
sys.exit(0)
fp = open(pathname, "rb")
try:
data = fp.read()
except MemoryError:
EasyDialogs.Message("Sorry, file is too big.")
sys.exit(0)
if len(data) == 0:
EasyDialogs.Message("File is empty.")
sys.exit(0)
number_cr = string.count(data, "\r")
number_lf = string.count(data, "\n")
if number_cr == number_lf == 0:
EasyDialogs.Message("File contains no lines.")
if number_cr == 0:
EasyDialogs.Message("File has unix-style line endings")
elif number_lf == 0:
EasyDialogs.Message("File has mac-style line endings")
elif number_cr == number_lf:
EasyDialogs.Message("File probably has MSDOS-style line endings")
else:
EasyDialogs.Message("File has no recognizable line endings (binary file?)")
sys.exit(0)
示例11: readworkingdirfile
def readworkingdirfile(fname):
# retun the contents of file fname
# if it is a dos file convert it to Mac
fp=open(getworkingdir()+fname,'rb')
try:
data = fp.read()
except MemoryError:
print 'Sorry, file is too big.'
return
if len(data) == 0:
print 'File is empty.'
return
number_cr = string.count(data, '\r')
number_lf = string.count(data, '\n')
#if number_cr == number_lf == 0:
#EasyDialogs.Message('File contains no lines.')
if number_cr == 0:
#EasyDialogs.Message('File has unix-style line endings')
data=string.replace(data,'\n','\r') #make it a mac file
#elif number_lf == 0:
#EasyDialogs.Message('File has mac-style line endings')
elif number_cr == number_lf:
#EasyDialogs.Message('File probably has MSDOS-style line endings')
data=string.replace(data,'\n','') #make it a mac file
#else:
#EasyDialogs.Message('File has no recognizable line endings (binary file?)')
#f=open('Macintosh HD:Python 2.0:santosfiles:gumbhere1','w')
#f.write(data)
fp.close()
return data
示例12: load
def load(self,name):
"""
Returns a two dimensional numpy array where a[:,0] is
wavelength in Angstroms and a[:,1] is flux in
counts/sec/angstrom/cm^2
Noisy spectra are smoothed with window_len in the .txt file.
Ergs and AB Mag units are automatically converted to counts.
"""
fname = self.objects[name]['dataFile']
fullFileName = os.path.join(self.this_dir,"data",fname[0])
if (string.count(fullFileName,"fit")):
a = self.loadSdssSpecFits(fullFileName)
else:
a = numpy.loadtxt(fullFileName)
len = int(self.objects[name]['window_len'][0])
if len > 1:
a[:,1] = smooth.smooth(a[:,1], window_len=len)[len/2:-(len/2)]
try:
fluxUnit = self.objects[name]['fluxUnit'][0]
scale = float(fluxUnit.split()[0])
a[:,1] *= scale
except ValueError:
print "error"
ergs = string.count(self.objects[name]['fluxUnit'][0],"ergs")
if ergs:
a[:,1] *= (a[:,0] * self.k)
mag = string.count(self.objects[name]['fluxUnit'][0],"mag")
if mag:
a[:,1] = (10**(-2.406/2.5))*(10**(-0.4*a[:,1]))/(a[:,0]**2) * (a[:,0] * self.k)
return a
示例13: parseResults
def parseResults(output):
results = {}
for line in output:
print line,
if string.count(line, "SIGSEG"):
results[0] = ["FAULT", string.strip(line)]
continue
# look for something of the form:
# filename:line:message
msg = string.split(line,":",2)
if len(msg)<3: continue
if msg[0]!=inputfilename: continue
if len(msg[1])==0: continue
if not msg[1][0] in string.digits: continue
# it's in the right form; parse it
linenumber = int(msg[1])
msgtype = "UNKNOWN"
uppermsg = string.upper(msg[2])
if string.count(uppermsg,"ERROR"):
msgtype = "ERROR"
if string.count(uppermsg,"WARNING"):
msgtype = "WARNING"
msgtext = string.strip(msg[2])
ignore = 0
for ignoreExpr in ignoreExprList:
if re.search(ignoreExpr,msgtext)!=None:
ignore = 1
if not ignore:
results[linenumber]=[msgtype,string.strip(msg[2])]
return results
示例14: process_request
def process_request(self, req):
req.perm.assert_permission ('WIKI_MODIFY')
page_name = req.args['target_page'][req.args['target_page'].find('wiki')+5:]
p = WikiPage(self.env, page_name )
author_name = req.authname
comment_text = req.args['comment']
comment_parent = req.args['comment_parent']
dt = datetime.now()
comment_date = dt.strftime("%Y-%m-%d %H:%M:%S")
comment_id = "%032x" % random.getrandbits(128)
redirect_url = "%s%s#%s" % (req.base_path, req.args['target_page'],comment_id)
changeset_comment = "%s..." % comment_text[:20]
insertion_index = string.find( p.text, "=%s" % comment_parent )
if ( insertion_index != -1 ):
heads = string.count(p.text,"{{{#!WikiComments",0,insertion_index)
tails = string.count(p.text,"}}}",0,insertion_index)
level = heads - tails
padding = ""
comment_out = '%s{{{#!WikiComments author="%s" date="%s" id="%s""\n%s%s\n%s=%s\n%s}}}\n' \
% (padding, author_name,comment_date,comment_id,padding,comment_text,padding,comment_id,padding)
p.text = p.text[:insertion_index]+comment_out+p.text[insertion_index:]
p.save( author_name, changeset_comment, req.remote_addr )
req.redirect(redirect_url)
示例15: extratabs
def extratabs(self, line):
tabcount = 0
for c in line:
if c <> "\t":
break
tabcount = tabcount + 1
last = 0
cleanline = ""
tags = PyFontify.fontify(line)
# strip comments and strings
for tag, start, end, sublist in tags:
if tag in ("string", "comment"):
cleanline = cleanline + line[last:start]
last = end
cleanline = cleanline + line[last:]
cleanline = string.strip(cleanline)
if cleanline and cleanline[-1] == ":":
tabcount = tabcount + 1
else:
# extra indent after unbalanced (, [ or {
for open, close in (("(", ")"), ("[", "]"), ("{", "}")):
count = string.count(cleanline, open)
if count and count > string.count(cleanline, close):
tabcount = tabcount + 2
break
return tabcount