零基础python29讲
f = open('F:\\record.txt')boy = []
girl = []
count = 1
for each_line in f:
if each_line[:6] != '======':
(name, content) = each_line.split(':',1)
if name == '小甲鱼':
boy.append(content)
else:
girl.append(content)
else:
boy_file_name = 'boy_' + str(count) + '.txt'
girl_file_name = 'boy_' + str(count) + '.txt'
boy_file = open(boy_file_name,'w')
girl_file = open(girl_file_name,'w')
boy_file.writelines(boy)
girl_file.writelines(girl)
boy_file.close()
girl_file.close()
boy = []
girl = []
count += 1
f.close()
求助,boy_file = open(boy_file_name,'w') 这里如果要指定文件的地址,应该怎么写呢?
girl_file = open(girl_file_name,'w') 把 boy_file_name 换成地址字符串。 boy_file = open('d:\\文件夹\\文件.txt','w') 永恒的蓝色梦想 发表于 2020-8-8 13:36
把 boy_file_name 换成地址字符串。
大佬,如果变成这样 boy_file = open('F:\\boy_file_name.txt','w') 那前面设置的变量就变成了了字符,结果生成
girl_file = open('F:\\girl_file_name.txt','w')
的文件名也是按boy_file_name.txt和girl_file_name.txt这样来了,而不是本来设置的按照count数分成123这样。有没有办法在变量生效的前提下指定文件位置呢? 好名字容易忘 发表于 2020-8-8 15:41
大佬,如果变成这样 boy_file = open('F:\\boy_file_name.txt','w') 那前面设置的变量就变成了了字符,结 ...
f = open('F:\\record.txt')
boy = []
girl = []
count = 1
for each_line in f:
if each_line[:6] != '======':
(name, content) = each_line.split(':',1)
if name == '小甲鱼':
boy.append(content)
else:
girl.append(content)
else:
boy_file = open(f'F:\\boy_{count}.txt','w')
girl_file = open(f'F:\\girl_{count}.txt','w')
boy_file.writelines(boy)
girl_file.writelines(girl)
boy_file.close()
girl_file.close()
boy = []
girl = []
count += 1
f.close() 永恒的蓝色梦想 发表于 2020-8-8 15:44
多谢大佬
页:
[1]