|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
为什么我的文件可已创建 但是内容为空?
代码是下面的
f = open("e:\\biancheng\\test.txt","r")
I = []
you = []
count = 1
for each_line in f:
if each_line[:6] != "======":
(role,speak) = each_line.split(":",1)
if role == "我":
I.append(speak)
if role == "你":
you.append(speak)
else:
file_name_I = "I_" + str(count) +".txt"
file_name_you = "you_" +str(count) + ".txt"
I_file = open(file_name_I,'w')
you_file = open(file_name_you,'w')
I_file.writelines(I)
you_file.writelines(you)
I_file.close()
you_file.close()
I=[]
you = []
count += 1
f.close()
本帖最后由 jackz007 于 2019-4-3 22:40 编辑
代码完全没有问题,现在需要怀疑的是汉字编码,用记事本打开 "e:\\biancheng\\test.txt",点击 "另存为" 看看文件编码,最好是 "ANSI" 编码格式,如果不是,那就另存成这种格式。
如果 test.txt 是utf8 编码,可以修改下面 2 句试试看:
- if role . decode("utf8") == "我":
- . . . . . .
- if role . decode("utf8") == "你":
复制代码
|
|