Python 如何编辑规格化函数,使其同时删除标点符号和行尾字符
如何编辑规格化函数,使其同时删除标点符号和行尾字符 代码示例如下:Python 如何编辑规格化函数,使其同时删除标点符号和行尾字符,python,Python,如何编辑规格化函数,使其同时删除标点符号和行尾字符 代码示例如下: filename="bible.Sentences.15.txt" def getData(filename): with open(filename,'r') as f: #converting to list where each element is an individual line of text file lines=[line.rs
filename="bible.Sentences.15.txt"
def getData(filename):
with open(filename,'r') as f:
#converting to list where each element is an individual line of text file
lines=[line.rstrip() for line in f]
return lines
filename="bibleSentences.txt"
getData(filename)
def normalize(filename):
#converting all letters to lowercase
lowercase_lines=[x.lower() for x in getData(filename)]
print(lowercase_lines)
return lowercase_lines
normalize(filename)
下面是一种方法:
import re
...
def normalize(data):
#converting all letters to lowercase
lowercase_lines=[x.lower() for x in data]
# strip out all non-word or tab or space characters
stripped_lines = [re.sub(r"[^\w \t]+", "", x) for x in lowercase_lines]
print(stripped_lines)
return stripped_lines
我尝试使用punctTable=str.maketrans(“”,,,string.标点符号),但它给出了一个错误。如果有人能帮上忙,我会非常感激的。。。请在文件中包含文本的最小示例。请阅读<代码>我尝试使用。。。str.maketrans-您应该在mre中包含该尝试,并解释为什么它不起作用,为什么结果与您预期的不同。
删除。。。行尾字符
-在获取数据
中,不是行.rstrip()
,这样做吗?正文的前几行是:开始时,上帝创造了天地。地球是没有形状和虚空的;黑暗笼罩着深渊的表面。神的灵在水面上移动。神说,要有光,就有光。神看见光明是好的,就把光明和黑暗分开。上帝称光为昼,称黑暗为夜。第一日是晚上和早晨。