[英]How do I get each line from a text file and split it so that I can use them all separately in python
[英]How can i get all 3 grams from a line fetched from a text file in python?
我从文本文件中提取了一行,结果生成了一行三克,但在行尾,其输出为两克。 例如输入行是cswisceduwwt 输出是
csw
swi
wis
isc
sce
ced
edu
dup
upa
par
ara
rad
ady
dyn
yn
在行的末尾,它产生2克(2个字符)。最后一个克是“ yn”,我认为它的添加空间。 我不需要“ yn”如何从每行中删除最后一个具有2个字符的克? 代码如下
def extract_n_grams(line):
ngram = ngrams(line, 3)
for item in ngram:
result=item[0]+item[1]+item[2]
print(result)
with open('C:/Users/Dania/Desktop/MS 2nd sem/preprocessed.txt') as corpus:
for line in corpus:
extract_n_grams(line)
它显示最后两个字符,因为它包含空格作为其last(3rd)字符,因此我使用此语句删除了行尾的空格
for line in corpus:
rem_line=line.rstrip('\n') #####removes space at the end of line
extract_n_grams(rem_line)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.