小鱼猫 发表于 2021-10-15 15:08:41

29节python课上的代码,运行的都不对,请大神看一下

test1 代码如下

f = open('C:\\Users\\Li xuexiu\\Desktop\\record.txt',encoding='UTF-8')
boy = []
girl = []
count = 1
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:
      file_name_boy = 'boy_'+str(count)+'.txt'
      file_name_girl = 'girl_'+str(count)+'.txt'
      
      boy_file = open('C:\\Users\\Li xuexiu\\Desktop\\file_name_boy.txt','w')
      girl_file = open('C:\\Users\\Li xuexiu\\Desktop\\file_name_girl.txt','w')

      boy_file.writelines(boy)
      girl_file.writelines(girl)

      boy_file.close()
      girl_file.close()

      boy = []
      girl = []
      count +=1
      
file_name_boy = 'boy_'+str(count)+'.txt'
file_name_girl = 'girl_'+str(count)+'.txt'

boy_file = open('C:\\Users\\Li xuexiu\\Desktop\\file_name_boy.txt','w')
girl_file = open('C:\\Users\\Li xuexiu\\Desktop\\file_name_girl.txt','w')

boy_file.writelines(boy)
girl_file.writelines(girl)

boy_file.close()
girl_file.close()
   
f.close()
运行后如图一




test2 上面的代码封装后
def save_file(boy,girl,count):
    file_name_boy = 'boy_'+str(count)+'.txt'
    file_name_girl = 'girl_'+str(count)+'.txt'
      
    boy_file = open('C:\\Users\\Li xuexiu\\Desktop\\file_name_boy.txt','w')
    girl_file = open('C:\\Users\\Li xuexiu\\Desktop\\file_name_girl.txt','w')

    boy_file.writelines(boy)
    girl_file.writelines(girl)

    boy_file.close()
    girl_file.close()
   
def split_file(file_name):
    file_name = open('C:\\Users\\Li xuexiu\\Desktop\\record.txt',encoding='UTF-8')
    boy = []
    girl = []
    count = 1
    for each_line in file_name:
      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)
            boy =[]
            girl = []
            count +=1
            save_file = (boy,girl,count)
            file_name.close()
               
            
split_file('C:\\Users\\Li xuexiu\\Desktop\\record.txt')
运行后如图二,一堆错误


            

逃兵 发表于 2021-10-15 15:25:52

31行
save_file = (boy,girl,count)
改成
save_file(boy,girl,count)

小鱼猫 发表于 2021-10-15 18:00:10

逃兵 发表于 2021-10-15 15:25
31行
save_file = (boy,girl,count)
改成


改了之后,出现下面的错误
Traceback (most recent call last):
File "C:\Users\Li Xuexiu\Desktop\pytnon 练习\python练习2021.9.22\029k1 三段对话六个文件升级版.py", line 35, in <module>
    split_file('C:\\Users\\Li xuexiu\\Desktop\\record.txt')
File "C:\Users\Li Xuexiu\Desktop\pytnon 练习\python练习2021.9.22\029k1 三段对话六个文件升级版.py", line 19, in split_file
    for each_line in file_name:
ValueError: I/O operation on closed file.

逃兵 发表于 2021-10-16 15:01:49

变量名和字符串你混用了,另外在运行过程中关闭了文件
def save_file(boy,girl,count):
    file_name_boy = 'boy_'+str(count)+'.txt'
    file_name_girl = 'girl_'+str(count)+'.txt'
      
    boy_file = open(file_name_boy,'w')
    girl_file = open(file_name_girl,'w')

    boy_file.writelines(boy)
    girl_file.writelines(girl)

    boy_file.close()
    girl_file.close()
   
def split_file(file_name):
    file_name = open(file_name)
    boy = []
    girl = []
    count = 1
    for each_line in file_name:
      if each_line [ : 6 ] != '======':
            (role,line_spoken)= each_line.split(':',1)
            if role == '小甲鱼':
                boy.append(line_spoken)
            if role == '小客服':
                girl.append(line_spoken)
            print(boy)
            print(girl)
      else:
            save_file(boy,girl,count)
            boy =[]
            girl = []
            count +=1
            save_file(boy,girl,count)
    file_name.close()
               
            
split_file('record.txt')

小鱼猫 发表于 2021-10-16 18:51:32

逃兵 发表于 2021-10-16 15:01
变量名和字符串你混用了,另外在运行过程中关闭了文件

{:10_287:}
页: [1]
查看完整版本: 29节python课上的代码,运行的都不对,请大神看一下