日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

FishC笔记—29 讲 文件:一个任务

發布時間:2023/12/20 编程问答 31 豆豆
生活随笔 收集整理的這篇文章主要介紹了 FishC笔记—29 讲 文件:一个任务 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

本期內容詳解:
任務:將文件(record.txt)中的數據進行分割并按照以下規律保存起來:
-小甲魚的對話單獨保存為boy_*.txt的文件(去掉“小甲魚:”)
-小客服的對話單獨保存為girl_*.txt的文件(去掉“小客服:”)
-文件中總共有三段對話,分別保存為boy_1.txt,girl_1.txt,boy_2.txt,
girl_2.txt,boy_3.txt,girl_3.txt共6個文件(提示:文件中的不同對話見已經使用“=========”分割)

可以從這里下載record.txt文檔:鏈接:http://pan.baidu.com/s/1sjzAhNR(密碼:tf2e)

def save_file(boy,girl,count): file_name_boy = 'boy_' + str(count) + '.txt' file_name_girl = 'girl_' + str(count) + '.txt' boy_file = open('D:\\%s'%file_name_boy,'w') girl_file = open('D:\\%s'%file_name_girl,'w') boy_file.writelines(boy) girl_file.writelines(girl) boy_file.close() girl_file.close() def split_file(file_name): f = open('D:\\record.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) count += 1 boy = [] girl = [] save_file(boy,girl,count) f.close() split_file('D:\\record.txt')

需要注意的幾點:
1、代碼

(role,line_spoken) = each_line.split(':',1)

其中,冒號應該是中文輸入法下的’:’,否則會報錯
2、直接在record.txt所在目錄下創建.py文件時,上述代碼中的打開文件操作可以直接用文件名而不需要指明路徑,即可以修改為:

f.open('record.txt')

上述兩個文件不在同一目錄下時,則應指明路徑

典型課后題
編寫一個程序,接受用戶的輸入并保存為新的文件,程序實現如圖:

論壇參考答案:

def file_writer(filename): f = open(filename,'w') print('請輸入內容【單獨輸入\':w\'保存退出】:') while True: write_some = input() if write_some != ':w': f.write('%s\n'%write_some) else: break f.close() file_name = input('請輸入文件名:') file_write(file_name)

編寫一個程序,比較用戶輸入的兩個文件,如果不同,顯示出所有不同處的行號與第一個不同字符的位置,程序實現如
圖:

參考答案:

def file_compare(file1,file2): f1 = open(file1) f2 = open(file2) count = 0#統計行數 differ = []#統計不一樣的數量 for line1 in f1: line2 = f2.readline() count += 1 if line1 != line2: differ.append(count) f1.close() f2.close() return differ file1 = input('請輸入需要比較的頭一個文件名:') file2 = input('請輸入需要比較的另一個文件名:') differ = file_compare(file1,file2) if len(differ) == 0: print('兩個文件完全一樣!') else: print('兩個文件共有【%d】處不同:'%len(differ)) for each in differ: print('第%d行不一樣'%each)

編寫一個程序,當用戶輸入文件名和行數(N)后,將該文件的前N行內容打印到屏幕上,程序實現如圖:

參考答案:

def file_view(file_name,line_nun): print('\n文件%s的前%s的內容如下:\n'%(file_name,line_num)) f = open(file_name) for i in range(int(line_num)): print(f.readline(),end='') f.close() file_name = input(r'請輸入要打開的文件(C:\\test.txt):') line_num = input('請輸入需要顯示該文件前幾行:') file_view(file_name,line_num)

呃,不得不說我們的用戶變得越來越刁鉆了。要求在上一題的基礎上擴展,用戶可以隨意輸入需要顯示的行數。(如輸
入13:21打印第13行到第21行,輸入:21打印前21行,輸入21:則打印從第21行開始到文件結尾所有內容)

論壇參考答案:

def file_view(file_name, line_num): if line_num.strip() == ':': begin = '1' end = '-1' (begin, end) = line_num.split(':') if begin == '': begin = '1' if end == '': end = '-1' if begin == '1' and end == '-1': prompt = '的全文' elif begin == '1': prompt = '從開始到%s' % end elif end == '-1': prompt = '從%s到結束' % begin else: prompt = '從第%s行到第%s行' % (begin, end) print('\n文件%s%s的內容如下:\n' % (file_name, prompt)) begin = int(begin) - 1 end = int(end) lines = end - begin f = open(file_name) for i in range(begin): # 用于消耗掉begin之前的內容 f.readline() if lines < 0: print(f.read()) else: for j in range(lines): print(f.readline(), end='') f.close() file_name = input(r'請輸入要打開的文件(C:\\test.txt):') line_num = input('請輸入需要顯示的行數【格式如 13:21 或 :21 或 21: 或 : 】:') file_view(file_name, line_num)

編寫一個程序,實現“全部替換”功能。
個人認為參考答案的原代碼有點問題,修改為以下形式:

def file_replace(file_name, rep_word, new_word): f_read = open(file_name) content = [] count = 0 for eachline in f_read: if rep_word in eachline: count += eachline.count(rep_word) #count感覺應該用這個 eachline = eachline.replace(rep_word, new_word) content.append(eachline) decide = input('\n文件 %s 中共有%s個【%s】\n您確定要把所有的【%s】替換為【%s】嗎?\n【YES/NO】:' \ % (file_name, count, rep_word, rep_word, new_word)) if decide in ['YES', 'Yes', 'yes']: f_write = open(file_name, 'w') f_write.writelines(content) f_write.close() f_read.close() file_name = input('請輸入文件名:') rep_word = input('請輸入需要替換的單詞或字符:') new_word = input('請輸入新的單詞或字符:') file_replace(file_name, rep_word, new_word)

總結

以上是生活随笔為你收集整理的FishC笔记—29 讲 文件:一个任务的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。