将文件sketch.txt的Man和Other Man说的台词分开输出。Man的台词输出到man_data.txt 。Other Man 的台词输出到other_data.txt。
Man: Is this the right room for an argument?
Other Man: I've told you once.
Man: No you haven't!
Other Man: Yes I have.
Man: When?
Other Man: Just now.
Man: No you didn't!
Other Man: Yes I did!
Man: You didn't!
Other Man: I'm telling you, I did!
Man: You did not!
Other Man: Oh I'm sorry, is this a five minute argument, or the full half hour?
Man: Ah! (taking out his wallet and paying) Just the five minutes.
Other Man: Just the five minutes. Thank you.
Other Man: Anyway, I did.
Man: You most certainly did not!
Other Man: Now let's get one thing quite clear: I most definitely told you!
Man: Oh no you didn't!
Other Man: Oh yes I did!
Man: Oh no you didn't!
Other Man: Oh yes I did!
Man: Oh look, this isn't an argument!
(pause)
Other Man: Yes it is!
Man: No it isn't!
(pause)
Man: It's just contradiction!
Other Man: No it isn't!
Man: It IS!
Other Man: It is NOT!
Man: You just contradicted me!
Other Man: No I didn't!
Man: You DID!
Other Man: No no no!
Man: You did just then!
Other Man: Nonsense!
Man: (exasperated) Oh, this is futile!!
(pause)
Other Man: No it isn't!
Man: Yes it is!
先写了一个打印格式的模块nester.py:
import sys
def print_lol(the_list,indent=False,level=0,fn=sys.stdout):
for each_item in the_list:
if isinstance(each_item,list):
print_lol(each_item,indent,level+1,fn)
else:
if indent:
for tab_stop in ranage(level):
print("\t",end='',file=fn)
print(each_item,file=fn)
在nester目录下(该目录下须有nester.py和setup.py),
For Linux/Unix
执行这个命令:
python3 setup.py sdist
python 3setup.py install
(注意,若是已经装过nester模块,则需要修改setup.py的版本,再执行上面的两行命令。
For windows下:
C:\Users\ThinkPad\AppData\Local\Programs\Python\Python36-32\nester> C:\Users\ThinkPad\AppData\Local\Programs\Python\Python36-32**python.exe** setup.py sdist
C:\Users\ThinkPad\AppData\Local\Programs\Python\Python36-32**python.exe** setup.py install
再来写一个处理剧本台词的代码:
import nester
import os
import sys
man=[ ]
other=[ ]
try:
data=open('sketch.txt')
for each_line in data:
try:
(role,line_spoken)=each_line.split(':',1)
line_spoken=line_spoken.strip()
if role == 'Man':
man.append(line_spoken)
elif role == 'Other Man':
other.append(line_spoken)
except ValueError:
pass
data.close()
except IOError:
nester.print_lol('The data file is missing!')
try:
with open('man_data.txt','w') as man_file:
nester.print_lol(man,fn=man_file)#将man保存到man_file
with open('other_data.txt','w') as other_file:
nester.print_lol(other,fn=other_file)
except IOError as err:
nester.print_lol('File error:
' + str(err))
最后执行:按F5.
可以在C:\Users\ThinkPad\AppData\Local\Progohtman_data.txt和other_data.txt。打开man_data.txt:
Is this the right room for an argument?
No you haven't!
When?
No you didn't!
You didn't!
You did not!
Ah! (taking out his wallet and paying) Just the five minutes.
You most certainly did not!
Oh no you didn't!
Oh no you didn't!
Oh look, this isn't an argument!
No it isn't!
It's just contradiction!
It IS!
You just contradicted me!
Youijust then!
(exasperated) Oh, this is futile!!
Yes it is!