本文整理汇总了Python中modules.unicode.encode函数的典型用法代码示例。如果您正苦于以下问题:Python encode函数的具体用法?Python encode怎么用?Python encode使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了encode函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: load_db
def load_db():
""" load lines from find.txt to search_dict """
if not os.path.isfile("find.txt"):
f = open("find.txt", "w")
f.write("#test,yano,foobar\n")
f.close()
search_file = open("find.txt", "r")
lines = search_file.readlines()
search_file.close()
search_dict = dict()
for line in lines:
line = uc.decode(line)
line = uc.encode(line)
a = line.replace(r'\n', '')
new = a.split(r',')
if len(new) < 3: continue
channel = uc.encode(new[0])
nick = new[1]
if len(new) < 2: continue
if channel not in search_dict:
search_dict[channel] = dict()
if nick not in search_dict[channel]:
search_dict[channel][nick] = list()
if len(new) > 3:
result = ",".join(new[2:])
result = result.replace('\n','')
elif len(new) == 3:
result = new[-1]
if len(result) > 0:
result = result[:-1]
if result:
search_dict[channel][nick].append(uc.decode(result))
return search_dict
示例2: rmuser
def rmuser(self, jenni, input, line):
if not input.admin:
return
if len(line) < 9:
jenni.reply("No input provided.")
return
line = line[8:].split()
channel = uc.encode((input.sender).lower())
nick = uc.encode(line[0]).lower()
def check(nick, channel):
nick = nick.lower()
channel = channel.lower()
if channel in self.scores_dict:
if nick in self.scores_dict[channel]:
del self.scores_dict[channel][nick]
return self.STRINGS["rmuser"].format(nick, channel)
else:
return self.STRINGS["nouser"].format(nick, channel)
else:
return self.STRINGS["nochan"].format(channel)
if len(line) == 1:
## .rmuser <nick>
result = check(nick, (input.sender).lower())
self.save()
elif len(line) == 2:
## .rumser <channel> <nick>
result = check(line[1], nick)
self.save()
jenni.say(result)
示例3: setpoint
def setpoint(self, jenni, input, line):
if not input.admin:
return
line = line[10:].split()
if len(line) != 4:
return
channel = uc.encode(line[0]).lower()
nick = uc.encode(line[1]).lower()
try:
add = int(line[2])
sub = int(line[3])
except:
jenni.say(self.STRINGS["invalid"])
return
if add < 0 or sub < 0:
jenni.reply("You are doing it wrong.")
return
if channel not in self.scores_dict:
self.scores_dict[channel] = dict()
self.scores_dict[channel][nick] = [int(add), int(sub)]
self.save()
jenni.say(self.str_score(nick, channel))
示例4: delquote
def delquote(code, input):
'''delquote <number> -- removes a given quote from the database. Can only be done by the owner of the bot.'''
text = input.group(2)
number = int()
try:
fn = open('quotes.txt', 'r')
except:
return code.reply('{red}No quotes to delete.')
lines = fn.readlines()
fn.close()
try:
number = int(text)
except:
code.reply('Please enter the quote number you would like to delete.')
return
newlines = lines[:number - 1] + lines[number:]
fn = open('quotes.txt', 'w')
for line in newlines:
txt = uc.encode(line)
if txt:
fn.write(txt)
if txt[-1] != '\n':
fn.write('\n')
fn.close()
code.reply('{green}Successfully deleted quote {b}%s{b}.' % (number))
示例5: collectlines
def collectlines(jenni, input):
"""Creates a temporary storage of most recent lines for s///"""
# don't log things in PM
#channel = (input.sender).encode("utf-8")
channel = uc.decode(input.sender)
channel = uc.encode(channel)
nick = (input.nick).encode("utf-8")
if not channel.startswith('#'): return
search_dict = load_db()
if channel not in search_dict:
search_dict[channel] = dict()
if nick not in search_dict[channel]:
search_dict[channel][nick] = list()
templist = search_dict[channel][nick]
line = input.group()
if line.startswith("s/"):
return
elif line.startswith("\x01ACTION"):
line = line[:-1]
templist.append(line)
else:
templist.append(line)
del templist[:-10]
search_dict[channel][nick] = templist
save_db(search_dict)
示例6: fucking_weather
def fucking_weather(jenni, input):
""".fw (ZIP|City, State) -- provide a ZIP code or a city state pair to hear about the fucking weather"""
## thefuckingweather.com website is not very reliable, in fat this often breaks due to their site
## not returning correct data
text = input.group(2)
if not text:
jenni.reply('INVALID FUCKING INPUT. PLEASE ENTER A FUCKING ZIP CODE, OR A FUCKING CITY-STATE PAIR.')
return
new_text = str()
new_text = uc.encode(text)
search = urllib.quote((new_text).strip())
url = 'http://thefuckingweather.com/?where=%s' % (search)
try:
page = web.get(url)
except:
return jenni.say("I COULDN'T ACCESS THE FUCKING SITE.")
## hacky, yes, I know, but the position of this information has yet to change
re_mark = re.compile('<p class="remark">(.*?)</p>')
re_temp = re.compile('<span class="temperature" tempf="\S+">(\S+)</span>')
re_condition = re.compile('<p class="large specialCondition">(.*?)</p>')
re_flavor = re.compile('<p class="flavor">(.*?)</p>')
re_location = re.compile('<span id="locationDisplaySpan" class="small">(.*?)</span>')
## find relevant information
temps = re_temp.findall(page)
remarks = re_mark.findall(page)
conditions = re_condition.findall(page)
flavor = re_flavor.findall(page)
new_location = re_location.findall(page)
## build return string
response = str()
if new_location and new_location[0]:
response += new_location[0] + ': '
if temps:
tempf = float(temps[0])
tempc = (tempf - 32.0) * (5 / 9.0)
response += u'%.1f°F?! %.1f°C?! ' % (tempf, tempc)
if remarks:
response += remarks[0]
else:
response += "THE FUCKING SITE DOESN'T CONTAIN ANY FUCKING INFORMATION ABOUT THE FUCKING WEATHER FOR THE PROVIDED FUCKING LOCATION. FUCK!"
if conditions:
response += ' ' + conditions[0]
if flavor:
response += ' -- ' + flavor[0].replace(' ', ' ')
jenni.say(response)
示例7: get_results
def get_results(text):
if not text:
return list()
a = re.findall(url_finder, text)
k = len(a)
i = 0
display = list()
passs = False
while i < k:
url = uc.encode(a[i][0])
url = uc.decode(url)
url = uc.iriToUri(url)
url = remove_nonprint(url)
domain = getTLD(url)
if '//' in domain:
domain = domain.split('//')[1]
if not url.startswith(EXCLUSION_CHAR):
passs, page_title = find_title(url)
if bitly_loaded:
bitly = short(url)
bitly = bitly[0][1]
else:
bitly = url
display.append([page_title, url, bitly])
i += 1
return passs, display
示例8: collectlines
def collectlines(jenni, input):
"""Creates a temporary storage of most recent lines for s///"""
#channel = (input.sender).encode("utf-8")
channel = uc.decode(input.sender)
channel = uc.encode(channel)
nick = (input.nick).encode("utf-8")
search_dict = load_db()
if channel not in search_dict:
search_dict[channel] = dict()
if 'last_said' not in search_dict[channel]:
search_dict[channel]['last_said'] = list()
if nick not in search_dict[channel]:
search_dict[channel][nick] = list()
templist = search_dict[channel][nick]
last_said_templist = search_dict[channel]['last_said']
line = input.group()
try:
line = (line).encode("utf-8")
except Exception:
return
if line.startswith("s/") or line.startswith('!'):
return
elif line.startswith("\x01ACTION"):
line = line[:-1]
templist.append(line)
last_said_templist.append("{}: {}".format(input.nick, line))
del templist[:-50]
del last_said_templist[:-50]
search_dict[channel][nick] = templist
search_dict[channel]['last_said'] = last_said_templist
save_db(search_dict)
示例9: delquote
def delquote(jenni, input):
'''.rmquote <number> -- removes a given quote from the database. Can only be done by the owner of the bot.'''
if not input.owner: return
text = input.group(2)
number = int()
try:
fn = open('quotes.txt', 'r')
except:
return jenni.reply('No quotes to delete.')
lines = fn.readlines()
MAX = len(lines)
fn.close()
try:
number = int(text)
except:
jenni.reply('Please enter the quote number you would like to delete.')
return
newlines = lines[:number-1] + lines[number:]
fn = open('quotes.txt', 'w')
for line in newlines:
txt = uc.encode(line)
if txt:
fn.write(txt)
if txt[-1] != '\n':
fn.write('\n')
fn.close()
jenni.reply('Successfully deleted quote %s.' % (number))
示例10: addquote
def addquote(code, input):
'''addquote <nick> something they said here -- adds the quote to the quote database.'''
fn = open('quotes.txt', 'a')
output = uc.encode(input.group(2))
fn.write(output)
fn.write('\n')
fn.close()
code.reply('Quote added.')
示例11: addquote
def addquote(jenni, input):
'''.addquote <nick> something they said here -- adds the quote to the quote database.'''
text = input.group(2)
fn = open('quotes.txt', 'a')
output = uc.encode(text)
fn.write(output)
fn.write('\n')
fn.close()
jenni.reply('Quote added.')
示例12: given_user
def given_user(nick, channel):
nick = uc.encode(nick.lower())
channel = channel.lower()
if channel in self.scores_dict:
if nick in self.scores_dict[channel]:
return self.str_score(nick, channel)
else:
return self.STRINGS["nouser"].format(nick, channel)
else:
return self.STRINGS["nochan"].format(channel)
示例13: save_db
def save_db(search_dict):
""" save search_dict to find.txt """
search_file = open("find.txt", "w")
for channel in search_dict:
if channel is not "":
for nick in search_dict[channel]:
for line in search_dict[channel][nick]:
channel_utf = uc.encode(channel)
search_file.write(channel_utf)
search_file.write(",")
nick = uc.encode(nick)
search_file.write(nick)
search_file.write(",")
line_utf = line
if not type(str()) == type(line):
line_utf = uc.encode(line)
search_file.write(line_utf)
search_file.write("\n")
search_file.close()
示例14: f_lisp
def f_lisp(jenni, input):
'''.lisp <lisp code> -- execute arbitrary lisp code'''
txt = input.group(2)
txt = uc.encode(txt)
try:
output = parse(txt)
output = eval(output)
output = to_string(output)
except Exception, e:
jenni.reply('Scheme Lisp ERROR: %s' % (str(e)))
return
示例15: addquote
def addquote(phenny, input):
'''.addquote <nick> something they said here -- adds the quote to the quote database.'''
text = input.group(2)
if not text:
return phenny.say('No quote provided')
fn = open('quotes.txt', 'a')
output = uc.encode(text)
fn.write(output)
fn.write('\n')
fn.close()
phenny.reply('Quote added.')