|
|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
在做小甲鱼29课视频中讲到的字符串分割
(name,speak)=each.split(':')
会报错:
(Traceback (most recent call last):
File "C:/Users/Administrator/Desktop/29.py", line 7, in <module>
(name,speak)=each.split(':')
ValueError: too many values to unpack (expected 2)
>>> )
但是我在shell中做的时候
>>> str1='小客服:那个有条回复“左手拿著小甲魚,右手拿著打火機,哪裡不會點哪裡,so easy ^_^”'
>>> (name,spoken)=str1.split(':')
>>> name
'小客服'
>>> spoken
'那个有条回复“左手拿著小甲魚,右手拿著打火機,哪裡不會點哪裡,so easy ^_^”'
>>>
却没有问题
初学Python 忘前辈解惑,谢谢
试试这样:
- def save(boy, girl, count):
- boy_filename = 'boy_' + str(count) + '.txt'
- girl_filename = 'girl' + str(count) + '.txt'
- boyfile = open(boy_filename, 'w')
- boyfile.writelines(boy)
- girlfile = open(girl_filename, 'w')
- girlfile.writelines(girl)
- boyfile.close()
- girlfile.close()
- def openfile(filename):
- f = open(filename)
- x = 1
- boy = []
- girl = []
- for each in f:
- if each[:6] != '======':
- (name, speak) = each.split(':', 1)
- if name == '小甲鱼':
- boy.append(speak)
- elif name == '小客服':
- girl.append(speak)
- else:
- save(boy, girl, x)
- boy = []
- girl = []
- x += 1
- save(boy, girl, x)
- f.close()
- openfile('F:\\【视频教学】零基础入门学习Python\\028文件:因为懂你,所以永恒\\record.txt')
复制代码
|
|