0

我正在通过 读取 csv 文件csv.reader(),我想从各种符号或空格中读取值,并且以小写形式读取,例如:

a = " Kingston-(USB) = 1GB, 2GB, 32GB . . . "

像这样转换'a':

a = "kingstonusb1gb2gb32gb"

我只想要数据中的字母和数字。
应删除任何类型的符号。
我已经使用.lower()函数和.strip()函数来做到这一点。但是 strip 函数只是删除单词之间的空格,而我想删除每种符号。
谢谢。

4

3 回答 3

3

您可以使用正则表达式:

>>> import re
>>> a = " Kingston-(USB) = 1GB, 2GB, 32GB . . . "
>>> re.sub(r'[^a-z0-9]', '', a.lower())
'kingstonusb1gb2gb32gb'

或发电机:

>>> import string
>>> allowed = string.lowercase + string.digits
>>> ''.join(c for c in a.lower() if c in allowed)
'kingstonusb1gb2gb32gb'

或者快速的东西:

>>> import string
>>> allowed = set(string.lowercase + string.digits)
>>> ''.join(filter(allowed.__contains__, a.lower()))
'kingstonusb1gb2gb32gb'
于 2013-02-28T05:43:22.783 回答
3

如果您使用的是 ascii,则可以使用 str.translate 从小写字符串中删除所有无数字和字母的内容

>>> from string import ascii_letters, digits
>>> ascii = set(chr(e) for e in range(128))
>>> ascii_sans_alpha = ''.join(ascii.difference(ascii_letters + digits))
>>> a = " Kingston-(USB) = 1GB, 2GB, 32GB . . . "
>>> a.lower().translate(None,ascii_sans_alpha)
'kingstonusb1gb2gb32gb'

这有多快?使用 timeit 参考

>>> def mgilson(a, good = set(string.ascii_lowercase + string.digits)):
    return ''.join(x for x in a.lower() if x in good)

>>> def blender_re(a):
    return re.sub(r'[^a-z0-9]', '', a.lower())

>>> def blender_gen(a, allowed = string.lowercase + string.digits):
    return ''.join(c for c in a.lower() if c in allowed)

>>> def blender_fast(a, allowed = set(string.lowercase + string.digits)):
    return ''.join(filter(allowed.__contains__, a.lower()))

>>> def abhijit(a, ascii_sans_alpha = ''.join(set(chr(e) for e in range(128)).difference(ascii_letters + digits))):
    return a.lower().translate(None,ascii_sans_alpha)

>>> time_dict = {"abhijit": "from __main__ import string, a",
             "blender_re":"from __main__ import re, a, abhijit",
             "blender_gen": "from __main__ import string, a",
             "blender_fast": "from __main__ import string, a",
             "mgilson": "from __main__ import string, a"}
>>> for k, v in time_dict.items():
    t = timeit.Timer(stmt="{}(a)".format(k),setup = "{},{}".format(v,k))
    print "Timing for {} is {}".format(k, t.timeit(100000))


Timing for blender_fast is 0.573348026237
Timing for blender_re is 0.632169556846
Timing for blender_gen is 0.720916486331
Timing for mgilson is 0.586592185393
Timing for abhijit is 0.203489867547
>>> 
于 2013-02-28T06:02:07.703 回答
2

我可能会做类似的事情:

import string
good = set(string.ascii_lowercase + string.digits)
a = ''.join(x for x in a.lower() if x in good)

它可能不是最有效的,但它应该可以工作并且很容易阅读/理解。

于 2013-02-28T05:42:56.290 回答