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

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > python >内容正文

python

Python---实验八

發布時間:2023/12/1 python 31 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Python---实验八 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

1,現在有一份‘邀請函.txt’的空白文件,請在同級目錄下編寫一段代碼,寫入內容‘誠摯邀請您來參加本次宴會’。

with open(f'G:\study\Python\邀請函.txt',mode='w',encoding='utf-8') as y:y.write('誠摯邀請您來參加本次宴會')

效果圖如下:

2,在第一題的基礎上,添加上問候語和發件人,內容是’best regards 李雷’,讓內容是:

誠摯邀請您來參加本次宴會。
best regards
李雷

with open(f'G:\study\Python\邀請函.txt',mode='w',encoding='utf-8') as y:y.write('誠摯邀請您來參加本次宴會。\nbest regards\n李雷')

效果圖如下:

3,在第二題的基礎上,這封郵件需要發送給‘丁一’、‘王美麗’、‘韓梅梅’三位朋友,請在郵件內容開頭處添加收件人名字,并且生成相應名字的郵件。郵件內容應該為:

丁一:
誠摯邀請您來參加本次宴會
best regards
李雷

文件名為: 朋友姓名邀請函.txt

intimates = ['丁一','王美麗','韓梅梅'] with open('G:\study\Python\邀請函.txt',mode='r',encoding='utf-8') as y:includes = y.read()for intimate in intimates:with open('G:\study\Python\%s邀請函.txt'%intimate,mode='w',encoding='utf-8') as yy:yy.write('%s:\n'%intimate)yy.write(includes)

效果圖如下:

4,使用嵌套循環實現九九乘法表,并將乘法表的內容寫入到txt文件中。

with open('G:\study\Python\99乘法表.txt',mode='w',encoding='utf-8') as yy:for i in range(1,10):for j in range(1,i+1):yy.write('%d×%d=%-2d '%(i,j,i*j))yy.write('\n')

效果圖如下:

5,把記事本文件test.txt轉換城Excel2007+文件。假設test.txt文件中第一行為表頭,從第二行開始為實際數據,并且表頭和數據行中的不同字段信息都是用逗號分隔。

from openpyxl import Workbook def main(txtFileName):new_XlsxFileName = txtFileName[:-3] + 'xlsx'wb=Workbook()ws=wb.worksheets[0]with open(txtFileName,mode='r',encoding='utf-8') as y:for line in y:line = line.strip().split(',')ws.append(line)wb.save(new_XlsxFileName) main('G:\\study\\Python\\excel.txt')

效果圖如下:

6,編寫程序,檢查D:\文件夾及其子文件夾中是否存在一個名為temp.txt的文件。

from os import listdir from os.path import join,isdir def search(directory,fileName):dirs = [directory]print(dirs)while(dirs):current = dirs.pop(0)print(current)print(listdir(current))for subPath in listdir(current):if subPath == fileName:return Truepath = join(current,subPath)if isdir(path):dirs.append(path)return False print(search('G:\\study\\Python','excel.txt'))

效果圖如下:

創作挑戰賽新人創作獎勵來咯,堅持創作打卡瓜分現金大獎

總結

以上是生活随笔為你收集整理的Python---实验八的全部內容,希望文章能夠幫你解決所遇到的問題。

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