カスタムファイル処理関数print_を呼び出すlol出力を処理するファイルman_dataとother_data
6358 ワード
1.print_の変更lolパラメータspaceを追加
2.print_を呼び出すlol関数、man_dataとother_dataフォーマット出力
転載先:https://www.cnblogs.com/nester-liz/p/9532107.html
''' "nesterliz001.py" print——lol() , ,
( ) 。'''
import sys
def print_lol(the_list, indent=False, level=0, space=sys.stdout): #sys.stdout
''' , 'the_list', pytho ( )。 ( ) , , ,'''
# space , print_lol()
for each_item in the_list:
if isinstance(each_item, list):
print_lol(each_item, indent, level+1, space )
else:
if indent:
for tab_stop in range(level):
print("\t",end='', file=space)
print(each_item, file=space)
#print_lol(name,True)
2.print_を呼び出すlol関数、man_dataとother_dataフォーマット出力
from nester_liz001 import print_lol # print_lol
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) # man man[]
elif role == 'Other Man':
other.append(line_spoken) # other man
except ValueError: #
pass
if 'data' in locals(): # locals() ‘data’
data.close()
except IOError: #I/O
print('The data file is missing!')
# man1.txt other2.txt
try:
with open('man_data.txt', 'w') as man_data:
print_lol(man, space=man_data)
with open('other_data.txt', 'w') as other_data:
print_lol(other, space=other_data)
except IOError as err:
print('file error:' + str(err))
転載先:https://www.cnblogs.com/nester-liz/p/9532107.html