[英]i have 200 text file in hindi. want to remove white space the special character and find the find bigram and trigram in python
import os
dir=os.getcwd()
print(dir)
dir1=os.path.join(dir,"test")
filename=os.listdir(dir1)
bad_chars = [';', ':', '!', "*","#","%"]
for i in filename:
filepath=os.path.join(dir1,i) # the path
file=open(filepath,"r",encoding="utf8") #open first text file
read_=file.read()
fields = read_.split(" ")
print(fields)
file1=open(filepath,"w",encoding="utf8")
file2=open(filepath,"a",encoding="utf8")
for j in range(len(fields)):
for p in bad_chars :
fields[j].replace(i,' ')
file2.write(fields[j])
print ("Resultant list is : " , fields[j])
file.close()
file1.close()
file2.close()
I am trying to remove special character fro all the 200 text file我正在尝试从所有 200 个文本文件中删除特殊字符
this is the code for bigram which I found这是我找到的 bigram 代码
example my name is eshan.例如我的名字是峨山。 output my, name occurs 1 name,is occurs 1 is, advance occurs 1 occurance can be more then 1 according to text
输出 my, name 出现 1 name,is 出现 1 is, Advance 出现 1 次出现可以超过 1 根据文本
Try this way:试试这个方法:
for file in filename:
filepath=os.path.join(dir1,file)
with open('inp.txt','r+') as f:
texts = f.read()
for c in bad_chars:
texts=texts.replace(c,' ')
#write to the file
with open('inp.txt','w') as f:
f.write(texts)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.