Reputation: 1
I'm trying read multiple text files, doing word segmentation (use jieba
) and then save the results to CSV files respectively. It shows
AttributeError: '_io.TextIOWrapper' object has no attribute 'decode'
Thanks for anyone's help.
The python code is:
import jieba
import csv
import glob
list_of_files = glob.glob('C:/Users/user/Desktop/speech./*.txt')
for file_name in list_of_files:
FI = open(file_name, 'r')
FO = open(file_name, 'w')
seglist = jieba.cut(FI, cut_all=False)
w = csv.writer(FO)
w.writerows(seglist)
FI.close()
FO.close()
Upvotes: 0
Views: 4154
Reputation: 8066
It seems that you need to send bytes to cut and not a file object
try this code instead:
list_of_files = glob.glob('C:/Users/user/Desktop/speech./*.txt')
for file_name in list_of_files:
with open(file_name, 'rb') as f:
text = f.read()
seglist = jieba.cut(text, cut_all=False)
with open(file_name, 'w') as f:
w = csv.writer(f)
w.writerows(seglist)
Upvotes: 1
Reputation: 10401
From what I read from source code as example and jieba.cut definition it seems jieba.cut
needs string as parameter.
But you are giving an instance of file
.
seglist = jieba.cut(FI.read(), cut_all=False)
Fixed the issue from what I saw. (FI.read()
is the fix).
Btw, do not call variables like FI
/ FO
, this is valid name for constants or maybe classes, but not variable.
Explicit is better than implicit: prefere something like: file_output
& file_input
.
Upvotes: 0