本文整理汇总了Python中string.translate函数的典型用法代码示例。如果您正苦于以下问题:Python translate函数的具体用法?Python translate怎么用?Python translate使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了translate函数的20个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于我们的系统推荐出更棒的Python代码示例。
示例1: index_img_kw
def index_img_kw(img, title, description):
"""
Parses the the title and description and creates a frequency table,
then stores the frequencies into the Keywords table for the given
image_id
"""
frequencies = {}
title_kws = title.split()
des_kws = description.split()
for word in title_kws:
word = word.lower()
word = string.translate(word, None, string.punctuation)
if word not in STOP_WORDS:
frequencies[word] = frequencies[word] + 2 if word in frequencies else 2
for word in des_kws:
if word not in STOP_WORDS:
word = word.lower()
word = string.translate(word, None, string.punctuation)
frequencies[word] = frequencies[word] + 1 if word in frequencies else 1
# Save in database now for this image
try:
for entry, val in frequencies.items():
kw = Keywords()
kw.keyword = entry.lower()
kw.frequency = val
kw.image = img
kw.save()
except:
print sys.exc_info()
开发者ID:eudisd,项目名称:image-search,代码行数:35,代码来源:views.py
示例2: make_langs
def make_langs(country_name):
"""Generates all four answers for the primary language quiz question,
including the right answer. Returns as a set."""
langs = set()
#Make country objects for wrong answers from same continent
country_obj = Country.query.filter(Country.country_name == country_name).first()
right_langs = country_obj.languages
right_langs = str(right_langs)
right_langs = translate(right_langs, None, '{"}')
langs.add(right_langs)
if country_obj.continent_name == "Caribbean":
langs.add("English, Spanish")
continent = country_obj.continent_name
nearby_countries = Country.query.filter(Country.continent_name == continent, Country.country_name != country_name).all()
top_index = len(nearby_countries) - 1
print top_index
while len(langs) < 4:
index = randint(0, top_index)
wrong_lang = (nearby_countries[index]).languages
wrong_lang = str(wrong_lang)
wrong_lang = translate(wrong_lang, None, '{"}')
langs.add(wrong_lang)
print langs
print len(langs)
return langs
开发者ID:rooharrigan,项目名称:Athena,代码行数:30,代码来源:server.py
示例3: encrypt_all
def encrypt_all(password, method, op, data):
if method is not None and method.lower() == 'table':
method = None
if not method:
[encrypt_table, decrypt_table] = init_table(password)
if op:
return string.translate(data, encrypt_table)
else:
return string.translate(data, decrypt_table)
else:
import M2Crypto.EVP
result = []
method = method.lower()
(key_len, iv_len) = method_supported[method]
(key, _) = EVP_BytesToKey(password, key_len, iv_len)
if op:
iv = random_string(iv_len)
result.append(iv)
else:
iv = data[:iv_len]
data = data[iv_len:]
if method == 'salsa20-ctr':
cipher = encrypt_salsa20.Salsa20Cipher(method, key, iv, op)
elif method == 'rc4-md5':
cipher = encrypt_rc4_md5.create_cipher(method, key, iv, op)
else:
cipher = M2Crypto.EVP.Cipher(method.replace('-', '_'), key, iv,
op, key_as_bytes=0, d='md5',
salt=None, i=1, padding=1)
result.append(cipher.update(data))
return ''.join(result)
开发者ID:23niu,项目名称:shadowsocks-manyuser,代码行数:31,代码来源:encrypt.py
示例4: hashword
def hashword(plaintext):
"""
Munge a plaintext word into something else. Hopefully, the result
will have some mnemonic value.
"""
# get a list of random bytes. A byte will be randomly picked from
# this list when needed.
rb = getrandomlist()
# 0.25 chance of case being swapped
if rb[rb[0]] < 64:
plaintext = string.swapcase(plaintext)
# 0.50 chance of vowels being translated one of two ways.
if rb[rb[2]] > 127:
plaintext = string.translate(plaintext,
string.maketrans('aeiou AEIOU', '@3!0& 4#10%'))
else:
plaintext = string.translate(plaintext,
string.maketrans('aeiou AEIOU', '^#1$~ $3!0&'))
# 0.4 chance of some additional consonant translation
if rb[rb[4]] < 102:
plaintext = string.translate(plaintext,
string.maketrans('cglt CGLT', '(<1+ (<1+'))
# if word is short, add some digits
if len(plaintext) < 5:
plaintext = plaintext + `rb[5]`
# 0.2 chance of some more digits appended
if rb[rb[3]] < 51:
plaintext = plaintext + `rb[205]`
return plaintext
开发者ID:pruan,项目名称:TestDepot,代码行数:29,代码来源:makepassword.py
示例5: main
def main():
f=string.ascii_lowercase
t=f[2:]+f[:2]
trans_tables=string.maketrans(f,t)
encs="g fmnc wms bgblr rpylqjyrc gr zw fylb. rfyrq ufyr amknsrcpq ypc dmp. bmgle gr gl zw fylb gq glcddgagclr ylb rfyr'q ufw rfgq rcvr gq qm jmle. sqgle qrpgle.kyicrpylq() gq pcamkkclbcb. lmu ynnjw ml rfc spj."
print(string.translate(encs,trans_tables))
print(string.translate('map',trans_tables))
开发者ID:wujianguo,项目名称:pythonchallenge,代码行数:7,代码来源:1.py
示例6: word_histogram
def word_histogram(source):
"""Create histogram of normalized words (no punct or digits)
scale that in terms of percentage"""
hist = {}
trans = maketrans('','')
if type(source) in (StringType,UnicodeType): # String-like src
for word in split(source):
word = translate(word, trans, punctuation+digits)
word=word.lower()
if len(word) > 0:
hist[word] = hist.get(word,0) + 1
elif hasattr(source,'read'): # File-like src
try:
from xreadlines import xreadlines # Check for module
for line in xreadlines(source):
for word in split(line):
word = translate(word, trans, punctuation+digits)
word=word.lower()
if len(word) > 0:
hist[word] = hist.get(word,0) + 1
except ImportError: # Older Python ver
line = source.readline() # Slow but mem-friendly
while line:
for word in split(line):
word = translate(word, trans, punctuation+digits)
word=word.lower()
if len(word) > 0:
hist[word] = hist.get(word,0) + 1
line = source.readline()
else:
raise TypeError, \
"source must be a string-like or file-like object"
return hist
开发者ID:menkhus,项目名称:miner,代码行数:33,代码来源:histogram.py
示例7: seq2
def seq2(ch,start,end):
trans = string.maketrans('ATCGatcg','TAGCtagc')
if ch[0:2] == 'ch':
inFile = open('/netshare1/home1/people/hansun/Data/GenomeSeq/Human/ucsc.hg19.fasta.fa')
while True:
line1 = inFile.readline().strip()
line2 = inFile.readline().strip()
if line1:
if line1 == '>'+ch:
if start <= end:
seq = line2[start-1:end].upper()
else:
seq = string.translate(line2[end-1:start][::-1],trans).upper()
return seq
else:
break
inFile.close()
elif ch[0:2] == 'NC':
inFile = open('/netshare1/home1/people/hansun/Data/VirusesGenome/VirusesGenome.fasta.fa')
while True:
line1 = inFile.readline().strip()
line2 = inFile.readline().strip()
if line1:
if line1.find('>'+ch) == 0:
if start <= end:
seq = line2[start-1:end].upper()
else:
seq = string.translate(line2[end-1:start][::-1],trans).upper()
return seq
else:
break
inFile.close()
开发者ID:hanice,项目名称:SIBS,代码行数:32,代码来源:1-diagram.py
示例8: encrypt_all
def encrypt_all(password, method, op, data):
if method is not None and method.lower() == "table":
method = None
if not method:
[encrypt_table, decrypt_table] = init_table(password)
if op:
return string.translate(data, encrypt_table)
else:
return string.translate(data, decrypt_table)
else:
import M2Crypto.EVP
result = []
method = method.lower()
(key_len, iv_len) = method_supported[method]
(key, _) = EVP_BytesToKey(password, key_len, iv_len)
if op:
iv = random_string(iv_len)
result.append(iv)
else:
iv = data[:iv_len]
data = data[iv_len:]
cipher = M2Crypto.EVP.Cipher(
method.replace("-", "_"), key, iv, op, key_as_bytes=0, d="md5", salt=None, i=1, padding=1
)
result.append(cipher.update(data))
f = cipher.final()
if f:
result.append(f)
return "".join(result)
开发者ID:baobaopangzi88,项目名称:shadowsocks,代码行数:30,代码来源:encrypt.py
示例9: alleleCount
def alleleCount(baseList,refNuc):
table = string.maketrans('ATCG', 'TAGC')
pos = defaultdict(int)
neg = defaultdict(int)
if refNuc in ['A', 'T']:
for (base, isReverse) in baseList:
if isReverse: # negative strand
neg[string.translate(base, table)] += 1
else: # positive strand
pos[base] += 1
elif refNuc == 'C': # only negative strand
for (base, isReverse) in baseList:
if isReverse:
neg[string.translate(base, table)] += 1
elif refNuc == 'G': # only positive strand
for (base, isReverse) in baseList:
if not isReverse:
pos[base] += 1
aCount = pos['A'] + neg['A']
tCount = pos['T'] + neg['T']
cCount = pos['C'] + neg['C']
gCount = pos['G'] + neg['G']
total = aCount + tCount + cCount + gCount
posCov = sum([pos[base] for base in ['A', 'T', 'C', 'G']])
negCov = sum([neg[base] for base in ['A', 'T', 'C', 'G']])
return aCount, tCount, cCount, gCount, total, posCov, negCov
开发者ID:evalineju,项目名称:MethGo,代码行数:26,代码来源:snp.py
示例10: decoder3
def decoder3():
table = string.maketrans(
'abcdefghijklmnopqrstuvwxyz',
'cdefghijklmnopqrstuvwxyzab'
)
print string.translate(codedmessage, table)
print string.translate('map', table)
开发者ID:Crackpot,项目名称:gftop,代码行数:7,代码来源:Level01.py
示例11: encripta_maketrans
def encripta_maketrans(self, texto):
# Conjunto dos caracteres do alfabeto sob a lógica do Rot13
alfabeto = string.maketrans("ABCDEFGHIJKLMabcdefghijklmNOPQRSTUVWXYZnopqrstuvwxyz",
"NOPQRSTUVWXYZnopqrstuvwxyzABCDEFGHIJKLMabcdefghijklm")
# A função translate que faz a conversão
# recebe o que deve ser convertido também o nome da técnica a ser usado que neste caso é o rot_13
print string.translate(texto, alfabeto)
开发者ID:Gpzim98,项目名称:PythonCodes,代码行数:7,代码来源:Funcao.py
示例12: __find
def __find(self):
self.status(1, "searching")
self.__nfound = 0
for i, filename in enumerate(self.get_files()):
if i % 16 == 0:
self.status(self.__nfound)
try:
f = open(filename, 'r')
except IOError:
continue
for linenumber, line in enumerate(f):
if not self.__running:
self.__finished()
if BINARY_RE.match(line):
break
line = string.translate(line, all_chars, hi_bit_chars)
line = string.translate(line, hi_lo_table)
matches = self.__pattern.findall(line)
if len(matches):
self.__nfound = self.__nfound + len(matches)
if self.__nfound >= self.__options.maxresults:
self.__finished()
result = GrepResult(linenumber, filename, line, matches)
gtk.threads_enter()
self.emit('found', result)
gtk.threads_leave()
f.close()
self.__finished()
开发者ID:BackupTheBerlios,项目名称:pida-svn,代码行数:28,代码来源:grepper.py
示例13: scrape_links_and_wordlistify
def scrape_links_and_wordlistify(links, lower=False, verbose=1):
import nltk
import requests
import string
raw = ''
wordlist = {}
for site in links:
try:
if verbose == 1:
print '[+] fetching data from: ', site
if site.find('http://pastebin.com/') == 0:
raw = requests.get(site.replace('http://pastebin.com/', 'http://pastebin.com/raw.php?i=')).content
else:
raw = requests.get(site).content
if lower == False:
l = string.translate(nltk.clean_html(raw), string.maketrans(string.punctuation, ' ' * 32)).split()
freq_an(l, wordlist)
else:
l = string.lower(nltk.clean_html(raw))
l = string.translate(l, string.maketrans(string.punctuation, ' ' * 32)).split()
freq_an(l, wordlist)
except:
if verbose == 1:
print '[-] Skipping url: ', site
return wordlist
开发者ID:tkisason,项目名称:unhash,代码行数:25,代码来源:gwordlist.py
示例14: make_code
def make_code(self):
symbols = (string.ascii_uppercase + string.digits)
string.translate(symbols, None, 'OI')
random.seed()
code = ''
for i in range(10):
code += random.choice(symbols)
self.code = code
开发者ID:kdudkov,项目名称:kruiz,代码行数:8,代码来源:models.py
示例15: decoder4
def decoder4():
table = string.maketrans(
string.ascii_lowercase,
string.ascii_lowercase[2:] + string.ascii_lowercase[:2]
)
print string.translate(codedmessage, table)
print codedmessage.translate(table)
print string.translate('map', table)
开发者ID:Crackpot,项目名称:gftop,代码行数:8,代码来源:Level01.py
示例16: slp_cmp
def slp_cmp(a,b):
try:
a1 = string.translate(a,trantable)
except:
print "sansort.san_cmp. Problem with translate. a=",a.encode('utf-8')
exit(1)
b1 = string.translate(b,trantable)
return cmp(a1,b1)
开发者ID:funderburkjim,项目名称:elispsanskrit,代码行数:8,代码来源:sansort.py
示例17: string_module
def string_module():
import string
s="ab cd ef"
print string.capwords(s);
leet=string.maketrans('ace', '123');
print string.translate(s, leet,'fbd');
value={'var':'foo'}
t=string.Template('')
开发者ID:shenenlutc,项目名称:helloPython,代码行数:8,代码来源:test.py
示例18: MakeIDXMLConform
def MakeIDXMLConform(id):
"""
Make the name/id COLLADA XML/XSD conform.
See StripString and translateMap docu for more information.
"""
if len(id) > 0 and id[0] == "#":
return "#" + string.translate(id[1:], translateMap)
else:
return string.translate(id, translateMap)
开发者ID:jackytck,项目名称:street,代码行数:9,代码来源:cutils.py
示例19: fixname
def fixname(s, removeDots = True):
ss = s.encode('iso8859_2', 'ignore')
if removeDots :
x = string.translate(ss, nametrans, '?')
while x and len(x) and (x[-1] == '.' or x[-1] == ' ') :
x = x[:-1]
else :
x = string.translate(ss, nametrans)
return x
开发者ID:sasq64,项目名称:csdbspider,代码行数:9,代码来源:utils.py
示例20: main2
def main2():
# for improvement
import string
offset = 2
table = string.maketrans(string.ascii_lowercase,
string.ascii_lowercase[offset:] + string.ascii_lowercase[:offset])
src = "g fmnc wms bgblr rpylqjyrc gr zw fylb. rfyrq ufyr amknsrcpq ypc dmp. bmgle gr gl zw fylb gq glcddgagclr ylb rfyr'q ufw rfgq rcvr gq qm jmle. sqgle qrpgle.kyicrpylq() gq pcamkkclbcb. lmu ynnjw ml rfc spj."
print string.translate(src, table)
print string.translate("map", table)
开发者ID:towerjoo,项目名称:python_challenge,代码行数:9,代码来源:1.py
注:本文中的string.translate函数示例由纯净天空整理自Github/MSDocs等源码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。 |
请发表评论