一、人生苦短,我用Python
1、案例背景
生成字母對并寫入檔案,并將結果寫入檔案中,
檔案中每行為:
ab
cd
ef
gh
2、主要知識點
- 檔案讀寫
- 基礎語法
- zip 函式
- 字串步長截取
3、素材

二、代碼展示
創建一個py檔案夾
咱們先匯入需要用的模塊
import platform import string # 我給大家準備了這些資料:Python視頻教程、100本Python電子書、基礎、爬蟲、資料分析、web開發、機器學習、人工智能、面試題、Python學習路線圖、問題解答! # 都放在這個群咯 279199867
zip 函式: 將兩個序列合并
def two_letters(): with open("p009.txt", "w") as f: # 從第 1 個字母開始,步長為 2 形成字串, ace... str1 = string.ascii_lowercase[::2] # 從第 2 個字母開始,步長為 2 獲取字串, bdf... str2 = string.ascii_lowercase[1::2] for i, j in zip(str1, str2): print(i, j) f.write(i + j + "\n")
檔案目錄
py-009/
└── py009.py
全部代碼
import platform import string print("待到來年九月八,馬踏東京賞櫻花") print("實戰場景: 生成字母對檔案 ") def two_letters(): with open("p009.txt", "w") as f: # 從第 1 個字母開始,步長為 2 形成字串, ace... str1 = string.ascii_lowercase[::2] # 從第 2 個字母開始,步長為 2 獲取字串, bdf... str2 = string.ascii_lowercase[1::2] for i, j in zip(str1, str2): print(i, j) f.write(i + j + "\n") two_letters() print("當前 Python 版本", platform.python_version())
三、運行結果

兄弟們,今天的分享就到這,再見!
喜歡就點個贊再走唄!
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/499843.html
標籤:其他
上一篇:3D human pose estimation in video with temporal convolutions and semi-supervised training 論文理解
下一篇:go學習筆記(一)
