求救小寫輸入小寫輸出
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')
if role == '小甲魚':
boy.append(line_spoken.lower()) 執行完程式後筆記本中英文還是大寫無更改 本帖最后由 suchocolate 于 2020-12-4 15:54 编辑
beach2020 发表于 2020-12-4 15:06
執行完程式後筆記本中英文還是大寫無更改
我提示的代码是将输出到boy_xxx.txt里的改成小写,如果想修改源文件,那么在打开文件之前做如下操作:with open('test.txt') as f:
lines = f.readlines()
with open('test.txt', 'w') as f:
for line in lines:
f.write(line.lower())
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.lower())
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')
請幫幫 執行程式後boy_1
OS.ACCESS(PATH,MODE)
OS.CHDIR(PATH) word 檔 改存為 txt檔 後
小甲魚:os.tmpfile()
小客符:Python3 中已刪除。返回一個打開的模式為(w+b)的文件對象 .這文件對像沒有文件夾入口,沒有文件描述符,將會自動刪除。
小甲魚:os.tmpnam()
小客符:Python3 中已刪除。為創建一個臨時文件返回一個唯一的路徑
小甲魚:os.ttyname(fd)
小客符:返回一個字符串,它表示與文件描述符fd 關聯的終端設備。如果fd 沒有與終端設備關聯,則引發一個異常。
小甲魚:os.unlink(path)
小客符:刪除文件路徑
小甲魚:os.utime(path, times)
小客符:返回指定的path文件的訪問和修改的時間。
小甲魚:os.walk(top[, topdown=True[, onerror=None[, followlinks=False]]])
小客符:輸出在文件夾中的文件名通過在樹中游走,向上或者向下。
小甲魚:os.write(fd, str)
小客符:寫入字符串到文件描述符 fd中. 返回實際寫入的字符串長度
小甲魚:os.path 模塊
小客符:獲取文件的屬性信息。
小甲魚:os.pardir()
小客符:獲取當前目錄的父目錄,以字符串形式顯示目錄名。
=================================================================
在word中英文字為小寫改存txt檔後
筆記本內容中英文字都會自動變成大寫儲存 本帖最后由 suchocolate 于 2020-12-4 16:03 编辑
beach2020 发表于 2020-12-4 15:53
def save_file(boy, girl, count): #封裝保存文件#組成涵數save_file
file_name_boy = 'boy_' + str(c ...
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
# 转换大小为小写
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)
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') 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
with open(file_name) as f:
lines = f.readlines()
with open(file_name, 'w') as f:
for line in lines:
f.write(line.lower())
with open(file_name) as f:
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())
elif role == '小客符':
girl.append(line_spoken)
else:
save_file(boy, girl, count)
boy = []
girl = []
count += 1
save_file(boy, girl, count)
f.close()
if __name__ == '__main__':
split_file('D:\\空白1.txt')
蟹蟹 但是還是一樣在筆記本為大寫 beach2020 发表于 2020-12-4 16:41
def save_file(boy, girl, count): #封裝保存文件#組成涵數save_file
file_name_boy = 'boy_' + str(c ...
我这里没有问题,你的代码和我的不一样,对一下吧。
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') 蟹蟹感謝
页:
[1]