Bot = commands.Bot(command_prefix= '!')
...
...
@Bot.event
async def <тип события>():
channel = Bot.get_channel(id)
await channel.send(переменная с вашим сообщением)
a = u'1a'
a = a.decode('utf-8').encode('cp1251')
print a
a = u'1\u0430'
a = a.encode('UTF-8').decode('UTF-8').encode('cp1251')
print a
#~ Не нужно путать юникод и UTF-8 !!! utf-8, как и cp1251 с точки зрения питона2.* - массив байт.
msg=u'1\u0430'
if len(str(msg))!==0
if isinstance(msg, unicode):
#~ print type(msg)
#~ print encodings(get_codepage(msg))
print msg.encode('UTF-8').decode('UTF-8').encode('cp1251')
else:
if get_codepage(msg)=='UTF-8':
print msg.decode('UTF-8').encode('cp1251')
else:
print msg
encodings = {
'UTF-8': 'utf-8',
'CP1251': 'windows-1251',
'KOI8-R': 'koi8-r',
'IBM866': 'ibm866',
'ISO-8859-5': 'iso-8859-5',
'MAC': 'mac',
}
def get_codepage(str = None):
uppercase = 1
lowercase = 3
utfupper = 5
utflower = 7
codepages = {}
for enc in encodings.keys():
codepages[enc] = 0
if str is not None and len(str) > 0:
last_simb = 0
for simb in str:
simb_ord = ord(simb)
"""non-russian characters"""
if simb_ord < 128 or simb_ord > 256:
continue
"""UTF-8"""
if last_simb == 208 and (143 < simb_ord < 176 or simb_ord == 129):
codepages['UTF-8'] += (utfupper * 2)
if (last_simb == 208 and (simb_ord == 145 or 175 < simb_ord < 192)) \
or (last_simb == 209 and (127 < simb_ord < 144)):
codepages['UTF-8'] += (utflower * 2)
"""CP1251"""
if 223 < simb_ord < 256 or simb_ord == 184:
codepages['CP1251'] += lowercase
if 191 < simb_ord < 224 or simb_ord == 168:
codepages['CP1251'] += uppercase
"""KOI8-R"""
if 191 < simb_ord < 224 or simb_ord == 163:
codepages['KOI8-R'] += lowercase
if 222 < simb_ord < 256 or simb_ord == 179:
codepages['KOI8-R'] += uppercase
"""IBM866"""
if 159 < simb_ord < 176 or 223 < simb_ord < 241:
codepages['IBM866'] += lowercase
if 127 < simb_ord < 160 or simb_ord == 241:
codepages['IBM866'] += uppercase
"""ISO-8859-5"""
if 207 < simb_ord < 240 or simb_ord == 161:
codepages['ISO-8859-5'] += lowercase
if 175 < simb_ord < 208 or simb_ord == 241:
codepages['ISO-8859-5'] += uppercase
"""MAC"""
if 221 < simb_ord < 255:
codepages['MAC'] += lowercase
if 127 < simb_ord < 160:
codepages['MAC'] += uppercase
last_simb = simb_ord
idx = ''
max = 0
for item in codepages:
if codepages[item] > max:
max = codepages[item]
idx = item
return idx
>>> import collections
>>> pair = collections.namedtuple('pair',['cat','dog','text'])
>>> vlist=[]
>>> vlist.append(pair(900,2,'foo'))
>>> vlist.append(pair(500,4,'bar'))
>>> vlist.append(pair(300, 10, 'foobar'))
>>> vlist
[pair(cat=900, dog=2, text='foo'), pair(cat=500, dog=4, text='bar'), pair(cat=300, dog=10, text='foobar')]
>>> vlist.sort()
>>> vlist
[pair(cat=300, dog=10, text='foobar'), pair(cat=500, dog=4, text='bar'), pair(cat=900, dog=2, text='foo')]
def compare(x, y):
if x[0] < y[0]:
return -1
elif x[0] > y[0]:
return 1
else:
if x[1] > y[1]: #знаки инвертируются, надо сортировать по 2ой переменной в обратном порядке
return -1
elif x[1] < y[1]:
return 1
else:
return 0
subjects = {(30.0,1.0,'description'), ...}
sortedDict = sorted(subjects, cmp=compare)
Принцип сортировки следующий - одновременно cat должно быть начиная с самого меньшего и dog от самого большего
import re
import json
from ast import literal_eval
pattern = re.compile(r'data[= ]+(?P<dict>.*);')
raw = pattern.search(s).groupdict()
# if валидый JSON
j = json.loads(raw['dict'])
print(j['id'], j['url'])
# запасной вариант
# elif похоже на валидный python-dict
d = literal_eval(raw['dict'])
print(d['id'], d['url'])
for iitems in items:
for line in iitems.value.splitlines():
print(line)
>>> urllib.quote_plus('string_of_characters_like_these:$#@=?%^Q^$')
'string_of_characters_like_these%3A%24%23%40%3D%3F%25%5EQ%5E%24'
In [1]: import requests
In [2]: r = requests.get('http://example.com', params={'п': 'лол'})
In [3]: r.url
Out[3]: u'http://example.com/?%D0%BF=%D0%BB%D0%BE%D0%BB'