Reputation: 130
I want to write the output into the text files without single quotes and common just need a single space. "Data"(in the program) gives correct output but can't write into the text files.
#data clean
save_path = "C:/Users/bubai/Desktop/try/preprosesing/output/"
path = "C:/Users/bubai/Desktop/try/preprosesing/data/"
all_txt_files = os.listdir(path)
for txt in all_txt_files:
txt_dir = path + txt #name +path
with open(txt_dir, 'r',encoding="utf-8") as txt_file:
clean = txt_file.read()
#removing unwanted character
data = res.replace('\n', '')
tokens = nltk.word_tokenize(res)
data = []
for x in tokens:
if x not in stopword:
data.append(x)
print("removed stopword")
data = print(*data, sep =' ')
completeName = os.path.join(save_path, txt)
output_file = open(completeName,"w",encoding = "utf-8") #for write
output_file.write(str(data))
txt_file.close()
output_file.close()
my output
इंदिरा गोस्वामी परिचय मूल नाम मामोनी रायसम गोस्वामीजन्म नवंबर गुवाहाटी असमभाषा असमिया अंग्रेजीविधाएँ कहानी उपन्यास शोध आत्मकथामुख्य कृतियाँ उपन्यास चेनाबार नीलकंठी ब्रज अहिरन छिन्नमस्ता मामरे धारा तरोवाल दाताल हातीर उवे खोवा हावदा तेज अरु धूलि धूसरित पृष्ठ ब्लडस्टेंड पेजिज दक्षिणी कामरूप गाथा कहानी संग्रह चिनाकी मरम कइना हृदय एक नदीर नाम प्रिय गल्पो आत्मकथा आधा लेखा दस्तावेज शोध रामायण फ्रॉम गंगा टू ब्रह्मपुत्र सम्मान पुरस्कार प्रिंसिपल प्रिंस क्लाउस लाउरेट पुरस्कार डच सरकार अंतरराष्ट्रीय तुलसी पुरस्कार फ्लोरिडा अंतरराष्ट्रीय विश्वविद्यालय साहित्य अकादमी पुरस्कार असम साहित्य सभा पुरस्कार भारत निर्माण पुरस्कार सौहार्द पुरस्कार उत्तर प्रदेश हिंदी संस्थान कमलकुमारी फाउंडेशन पुरस्कार निधन नवंबर गुवाहाटी असम
Upvotes: 0
Views: 48
Reputation: 605
Try below code:
# Don't forget to import os, nltk
save_path = "C:/Users/bubai/Desktop/try/preprosesing/output/"
path = "C:/Users/bubai/Desktop/try/preprosesing/data/"
all_txt_files = os.listdir(path)
for txt in all_txt_files:
txt_dir = path + txt #name +path
with open(txt_dir, 'r',encoding="utf-8") as txt_file:
clean = txt_file.read()
#removing unwanted character
data = res.replace('\n', '')
tokens = nltk.word_tokenize(res)
data = []
for x in tokens:
if x not in stopword:
data.append(x)
print("removed stopword")
completeName = os.path.join(save_path, txt)
with open(completeName, "w") as f:
f.write(" ".join(data))
Upvotes: 2