【发布时间】:2013-01-04 06:09:49
【问题描述】:
我读到这个: Stripping everything but alphanumeric chars from a string in Python
还有这个: Python: Strip everything but spaces and alphanumeric
不太明白,但我尝试了一下自己的代码,现在看起来像这样:
import re
decrypt = str(open("crypt.txt"))
crypt = re.sub(r'([^\s\w]|_)+', '', decrypt)
print(crypt)
当我运行脚本时,它会返回以下答案: C:\Users\Adrian\Desktop\python>python tick.py ioTextIOWrapper namecrypttxt mode encodingcp1252
我试图从文档中删除所有额外的代码,只保留数字和字母,在文档中可以找到以下文本:http://pastebin.com/Hj3SjhxC
我正在尝试解决这里的任务: http://www.pythonchallenge.com/pc/def/ocr.html
有人知道“ioTextIOWrapper namecrypttxt moder encodingcp1252”是什么意思吗? 我应该如何格式化代码以正确地将其从除字母和数字之外的所有内容中剥离出来?
真诚的
【问题讨论】:
-
你也应该用
regex标记这个
标签: python regex string formatting