|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
- f = open('E:\\1.txt',encoding = 'utf-8')
- boy = []
- girl =[]
- count = 1
- for each_line in f:
- if each_line[:6] != '======':
- (name,sentenses)=each_line.split(':')
- if name == '男':
- boy.append(sentenses)
- if name == '女':
- girl.append(sentenses)
- else:
- file_name_boy = 'boy_' + str(count) +'.txt'
- file_name_girl = 'girl_' + str(count) +'.txt'
- file_boy = open(file_name_boy,'w')
- file_girl = open(file_name_girl,'w')
- file_boy.writelines(boy)
- file_girl.writelines(girl)
- file_boy.close()
- file_girl.close()
- boy = []
- girl = []
- count+=1
- file_name_boy = 'boy_' + str(count) +'.txt'
- file_name_girl = 'girl_' + str(count) +'.txt'
- file_boy = open('file_name_boy')
- file_girl = open('file_name_girl')
- file_boy.writelines(boy)
- file_girl.writelines(girl)
- file_boy.close()
- file_girl.close()
- f.close()
-
-
-
-
-
-
复制代码
想问下,以上代码有什么错误吗,运行一直显示错误:ValueError: not enough values to unpack (expected 2, got 1)
还有,
- file_name_boy = 'boy_' + str(count) +'.txt'
复制代码
count 为啥是字符串的形式,直接用count不行吗
求大神解答
本帖最后由 jackz007 于 2020-2-29 11:39 编辑
用记事本打开 'E:\\1.txt',仔细观察,文件内容必须满足以下条件,程序才能正常工作:
文件只能由两种文本行构成,一种是分隔行,内容完全由 6 个以上连续的 '#' 字符构成,另一种是信息行,行中必须有且仅有一个冒号,注意,冒号必须是半角的 ':' ,不是全角的 ':',文件中不可以有任何的空行存在。
|
|