|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
word文本
小甲魚:os.access(path, mode)
小客符:檢驗權限模式
小甲魚:os.chdir(path)
小客符:改變當前工作目錄
改成.txt格式儲存後英文字會自動變成大寫
小甲魚:OS.ACCESS(PATH,MODE)
小客符:檢驗權限模式
小甲魚:OS.CHDIR(PATH)
小客符:改變當前工作目錄
小甲魚:OS.CHFLAGS(PATH,FLAGS)
請問如何經過程式切片後保持文本英文小寫
def save_file(boy, girl, count): #封裝保存文件#組成涵數save_file
file_name_boy = 'boy_' + str(count) + '.txt'
file_name_girl = 'girl_' + str(count) + '.txt'
boy_file = open(file_name_boy, 'w')
girl_file = open(file_name_girl, 'w')
boy_file.writelines(boy)
girl_file.writelines(girl)
boy_file.close()
girl_file.close()
def split_file(file_name):#組成涵數split_file
f = open('D:\\空白1.txt')
boy = []
girl = []
count = 1
for each_line in f:
if each_line[:6] != '======':
#進行字符串分割操作
(role, line_spoken) = each_line.split(':', 1)
if role == '小甲魚':
boy.append(line_spoken)
if role == '小客符':
girl.append(line_spoken)
else:
save_file(boy, girl, count)
#文件分別保存操作
boy = []
girl = []
count += 1
save_file(boy, girl, count)
f.close()
split_file('D:\\空白1.txt')
我这里没有问题,你的代码和我的不一样,对一下吧。
- def save_file(boy, girl, count):
- file_name_boy = 'boy_' + str(count) + '.txt'
- file_name_girl = 'girl_' + str(count) + '.txt'
- boy_file = open(file_name_boy, 'w')
- girl_file = open(file_name_girl, 'w')
- boy_file.writelines(boy)
- girl_file.writelines(girl)
- boy_file.close()
- girl_file.close()
- def split_file(file_name):
- # 转换大小为小写
- with open(file_name, encoding='utf-8') as f:
- lines = f.readlines()
- with open(file_name, 'w', encoding='utf-8') as f:
- for line in lines:
- f.write(line.lower())
- f = open(file_name, encoding='utf-8')
- boy = []
- girl = []
- count = 1
- for each_line in f:
- if each_line[:6] != '======':
- (role, line_spoken) = each_line.split(':', 1)
- if role == '小甲魚':
- boy.append(line_spoken.lower())
- if role == '小客符':
- girl.append(line_spoken)
- else:
- save_file(boy, girl, count)
- boy = []
- girl = []
- count += 1
- save_file(boy, girl, count)
- if __name__ == '__main__':
- split_file('空白.txt')
复制代码
|
|