import re
with open('/Users/file.csv', 'rb') as file:
for line in file:
line = re.sub(" , [0-2][0-9]:[0-5][0-9]", "", line)
line = re.sub("[,|!|.|?|\"]", "", line)
words = [w.lower() for w in line.split()]
for word in words:
...
Lorem Ipsum is simply dummy "text" of the ,0
printing and typesetting; industry. Lorem,1
Ipsum has been the industry's standard ,2
dummy text ever since the 1500s, when an,3
unknown printer took a galley of type and,4
scrambled it to make a type specimen ,5
book. It has survived not only five ,6
centuries, but also the leap into electronic,7
typesetting, remaining essentially unch,8
anged. It was popularised in the 1960s with ,9
the release of Letraset sheets conta,10
ining Lorem Ipsum passages, and more rec,11
ently with desktop publishing software like,12
!!Aldus PageMaker!! including versions of,13
Lorem Ipsum.,14
你有你需要的东西。缺少的一点是小写转换,只需使用
word.lower()
即可完成。在另一件你缺少的事就是分词。您应该为这个任务使用
.split()
,默认情况下,它会拆分每个空格字符,即空格、制表符等您将遇到的一个问题是区分文本中的逗号和列分隔逗号。也许不要使用csv阅读器,只需阅读每一行并删除时间,然后将其拆分成单词。在
如果要删除其他字符,请将它们包含在第二个正则表达式中。如果性能对您很重要,您应该在
for
循环之前编译两个正则表达式一次。在pythonscsv模块是一个很好的库,但是经常使用它来完成更简单的任务可能会有点过头。 对我来说,这是一个典型的例子,使用csv模块可能会使事情过于复杂
对我来说
是一种线性的直接方法
使用以下文件内容运行的示例
^{pr2}$
相关问题 更多 >
编程相关推荐