小编Fre*_*red的帖子

删除重音和特殊字符

可能重复:
在python unicode字符串中删除重音的最佳方法是什么?
Python和字符规范化

我想删除重音,将所有字符都改为小写,并删除任何数字和特殊字符.

示例:

Frédér8ic@ - >弗雷德里克

提案:

def remove_accents(data):
    return ''.join(x for x in unicodedata.normalize('NFKD', data) if \
    unicodedata.category(x)[0] == 'L').lower()
Run Code Online (Sandbox Code Playgroud)

有没有更好的方法来做到这一点?

python diacritics

9
推荐指数
1
解决办法
2万
查看次数

二进制到字符串,比字典更好?

目标:将二进制转换为字符串

示例:0111010001100101011100110111010001100011011011110110010001100101 - > testCode(不含空格)

我使用字典和我的功能,我搜索更好的方式,更有效率

from textwrap import wrap

DICO = {'\x00': '00', '\x04': '0100', '\x08': '01000', '\x0c': '01100', 
'\x10': '010000', '\x14': '010100', '\x18': '011000', '\x1c': '011100',
' ': '0100000', '$': '0100100', '(': '0101000', ',': '0101100', '0': '0110000',
'4': '0110100', '8': '0111000', '<': '0111100', '@': '01000000',
'D': '01000100', 'H': '01001000', 'L': '01001100', 'P': '01010000',
'T': '01010100', 'X': '01011000', '\\': '01011100', '`': '01100000',
'd': '01100100', 'h': '01101000', 'l': '01101100', 'p': '01110000',
't': '01110100', 'x': '01111000', '|': '01111100', '\x03': '011',
'\x07': …
Run Code Online (Sandbox Code Playgroud)

python string binary dictionary

6
推荐指数
2
解决办法
1401
查看次数

ctypes vs纯python

为什么ctypes在我的代码中比纯python更慢以增加变量?

from ctypes import *
import timeit

def f1():
    global t
    t += 1

def f2():
    p[0] += 1

t = 0
n = c_int(0)
p = pointer(n)

print(timeit.timeit("f1()", setup="from __main__ import f1")) # 0.3417885800008662
print(timeit.timeit("f2()", setup="from __main__ import f2")) # 0.5280102270189673

print(t) # 1000000
print(n.value) # 1000000
Run Code Online (Sandbox Code Playgroud)

如何用ctypes模块提高速度?

python performance ctypes

2
推荐指数
1
解决办法
378
查看次数

删除函数bin(x)的结果'b'

如何正确删除函数bin(x)结果中的'b'

例:

bin(ord("\'"))
  -> '0b100111'
Run Code Online (Sandbox Code Playgroud)

但我想要这个结果

  -> '0100111'
Run Code Online (Sandbox Code Playgroud)

提前谢谢,

python binary

0
推荐指数
1
解决办法
1262
查看次数

标签 统计

python ×4

binary ×2

ctypes ×1

diacritics ×1

dictionary ×1

performance ×1

string ×1