鱼C论坛

 找回密码
 立即注册
查看: 597|回复: 6

错误修改

[复制链接]
发表于 2019-5-19 14:49:19 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
import pickle
def split(file):
    f = open(file)
    boy_word = []
    girl_word = []
    count = 0
    for each_line in f:
        if each_line[:6] != '======':
            (name, word) = each_line.split(':', 1)
            if name == '小甲鱼':
                boy_word.append(word)
            if name == '小客服':
                girl_word.append(word)
        else:
            count += 1
            file1 = 'boy_' + str(count), + '.txt'
            file2 = 'girl_' + str(count), +  '.txt'
            file_boy = open(file1, 'wb')
            file_girl = open(file2, 'wb')
            pickle.dump(boy_word, file_boy)
            pickle.dump(girl_word, file_girl)
            file_boy.close()
            file_girl.close()
            boy_word = []
            girl_word = []
    count += 1
    file1 = 'boy_' + str(count), + '.txt'
    file2 = 'girl_'+ str(count), +  '.txt'
    file_boy = open(file1, 'wb')
    file_girl = open(file2, 'wb')
    pickle.dump(boy_word, file_boy)
    pickle.dump(girl_word, file_girl)
    file_boy.close()
    file_girl.close()
    f.close()
temp = input('请输入需要分割的文件名:')
split(temp)



Traceback (most recent call last):
  File "C:\Users\10835\Desktop\python\IDLE.py", line 37, in <module>
    split(temp)
  File "C:\Users\10835\Desktop\python\IDLE.py", line 16, in split
    file1 = 'boy_' + str(count), + '.txt'
TypeError: bad operand type for unary +:
'str'



我的程序运行报出这种错误怎么修改呀。我和作业比对了一下,感觉没啥问题呀?
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2019-5-19 14:52:15 From FishC Mobile | 显示全部楼层
那个逗号很可疑
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2019-5-19 17:51:55 | 显示全部楼层
file1 = 'boy_' + str(count), + '.txt'
TypeError: bad operand type for unary +: 'str'

错误已经提示你了,类型错误, str(count)的后面有个逗号,然后后面接了个+号。

把逗号去掉试试。
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2019-5-19 18:44:27 | 显示全部楼层
heidern0612 发表于 2019-5-19 17:51
file1 = 'boy_' + str(count), + '.txt'
TypeError: bad operand type for unary +: 'str'

去掉逗号还是会报一样的错误
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2019-5-19 18:50:44 From FishC Mobile | 显示全部楼层
小白码农超 发表于 2019-5-19 18:44
去掉逗号还是会报一样的错误

不可能一样的信息错误
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2019-5-19 19:46:19 | 显示全部楼层
file1 = 'boy_' + str(count), + '.txt'
file2 = 'girl_'+ str(count), +  '.txt'
文件里有2处这两行代码。其中的逗号,都要去掉
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2019-5-19 20:00:34 | 显示全部楼层
wwhywhy 发表于 2019-5-19 19:46
file1 = 'boy_' + str(count), + '.txt'
file2 = 'girl_'+ str(count), +  '.txt'
文件里有2处这两行代码 ...

想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2024-5-8 11:18

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表