28讲。哪位大佬帮我看一下
本帖最后由 大大的拳头 于 2020-5-21 10:59 编辑照着小甲鱼的课件打得就是没有运行出效果
def save_file(boy, girl, count):
file_name_boy = "boy_"+str(count)+".txt"
file_name_girl = "girl" + str(count)+".txt"
boy_file = open((r"D:\PythonJY\pythonJY\pythonJY7-9\%s" %
file_name_boy), "w")
girl_file = open((r"D:\PythonJY\pythonJY\pythonJY7-9\%s" %
file_name_girl), "w")
boy_file.writelines(boy)
girl_file.writelines(girl)
boy_file.close()
girl_file.close()
def spilt_file(file_name):
count = 1
boy = []
girl = []
f = open(file_name)
for each_line in f:
if each_line[:6] != "======":
(role, line_spoken) = each_line.split(":", 1)
if role == "小甲鱼":
boy.append(line_spoken)
if role == "小客服":
girl.append(line_spoken)
else:
save_file(boy, girl, count)
count += 1
boy = []
girl = []
save_file(boy, girl, count)
f.close()
spilt_file(r"D:\PythonJY\pythonJY\pythonJY7-9\record.txt") 看这个:戳我前进 别用vs code运行,它有时候抽风
PS:这是编码格式错误,加一个编码格式设置,设置成utf-8 编码问题,open处加一个参数:
encoding="utf-8"
求最佳~ open( 里面加上encoding='utf-8')就可以了 还有,5~8行
boy_file = open((r"D:\PythonJY\pythonJY\pythonJY7-9\%s" %
file_name_boy), "w")
girl_file = open((r"D:\PythonJY\pythonJY\pythonJY7-9\%s" %
file_name_girl), "w")
和最后一行
spilt_file(r"D:\PythonJY\pythonJY\pythonJY7-9\record.txt")
这里因为反斜杠是转义字符,所以要表示反斜杠要用反斜杠对自身转义,也就是'\\'
def save_file(boy, girl, count):
file_name_boy = "boy_"+str(count)+".txt"
file_name_girl = "girl" + str(count)+".txt"
boy_file = open((r"D:\\PythonJY\\pythonJY\\pythonJY7-9\\%s" %
file_name_boy), "w")
girl_file = open((r"D:\\PythonJY\\pythonJY\\pythonJY7-9\\%s" %
file_name_girl), "w")
boy_file.writelines(boy)
girl_file.writelines(girl)
boy_file.close()
girl_file.close()
def spilt_file(file_name):
count = 1
boy = []
girl = []
f = open(file_name)
for each_line in f:
if each_line[:6] != "======":
(role, line_spoken) = each_line.split(":", 1)
if role == "小甲鱼":
boy.append(line_spoken)
if role == "小客服":
girl.append(line_spoken)
else:
save_file(boy, girl, count)
count += 1
boy = []
girl = []
save_file(boy, girl, count)
f.close()
spilt_file(r"D:\\PythonJY\\pythonJY\\pythonJY7-9\\record.txt") KevinHu 发表于 2020-5-21 11:18
还有,5~8行
和最后一行
或者使用斜杠'/'也可以{:10_256:} 本帖最后由 大大的拳头 于 2020-5-21 13:58 编辑
.
页:
[1]