python中reader_Python檔案讀寫教程
阿新 • • 發佈:2020-12-18
技術標籤:python中readervbnet讀取txt檔案全部內容怎麼用object file library格式開啟檔案
在日常工作中,我們與檔案打交道的時間比較長,因此熟練掌握檔案的操作必不可少,今天給大家整理出利用Python進行檔案讀寫操作的相關內容,希望對大家的工作或學習提供一些參考,本文給大家列舉出以下三種常見檔案格式
- 1.CSV
- 2.txt
- 3.Excel
下面,我開始逐一介紹
1.CSV檔案
參考文件:https://docs.python.org/zh-cn/3/library/csv.html
1.1 CSV寫
實操程式碼
import csvwith open(file="demo.csv", mode="w", encoding="utf-8") as file: file.write('Those times when you get up early and you work hard\n' 'Those times when you stay up late and you work hard\n' 'Those times when you don\'t feel like working you are too tired\n' 'You don\'t want to push yourself but you do it anyway\n' 'That is actually the DREAM!')
程式碼執行結果
Those times when you get up early and you work hardThose times when you stay up late and you work hardThose times when you don't feel like working you are too tiredYou don't want to push yourself but you do it anywayThat is actually the DREAM!
檔案開啟效果
1.2 CSV讀
實操程式碼
import csvwith open(file='demo.csv', mode='r', encoding='utf-8', newline='') as file: reader = csv.reader(file) for i in reader: print(i)
程式碼執行效果
['Those times when you get up early and you work hard']['Those times when you stay up late and you work hard']["Those times when you don't feel like working you are too tired"]["You don't want to push yourself but you do it anyway"]['That is actually the DREAM!']
2.txt檔案
2.1 txt寫
實操程式碼
with open(file="demo.txt", mode="w", encoding="utf-8") as file: file.writelines(('Python之路\n', '任重道遠\n', '再苦再難\n', '也別放棄'))
程式碼執行效果
Python之路任重道遠再苦再難也別放棄
檔案開啟效果
2.2 txt讀
實操程式碼
with open(file="demo.txt", mode="r", encoding="utf-8") as file: # read方法讀取檔案全部內容 print(file.read()) # readline方法讀取檔案第一行內容 print(file.readline()) # readlines方法讀取檔案每一行內容 reader = file.readlines() print(f'readlines方法直接讀取檔案內容:{reader}') f = [] for i in reader: f.append(i.replace('\n', '')) print(f'對獲取的檔案內容做一下簡單的處理以後:{f}')
程式碼執行效果
readlines方法直接讀取檔案內容:['Python之路\n', '任重道遠\n', '再苦再難\n', '也別放棄']對獲取的檔案內容做一下簡單的處理以後:['Python之路', '任重道遠', '再苦再難', '也別放棄']
3.Excel檔案
3.1 Excel寫
實操程式碼
import numpy as npfrom openpyxl import Workbookfrom openpyxl.workbook.workbook import Worksheetworkbook = Workbook()worksheet: Worksheet = workbook['Sheet']header = [ ['Name'], ['Age'], ['Gender'], ['Plans']]value = [ ['Michael'], [28], ['Male'], ['I\'m studying Python']]header = np.array(header)value = np.array(value)header_input = []for hd in range(len(header)): header_input.append(header[hd][0])worksheet.append(header_input)for col in range(len(value[0])): worksheet.append(value[:, col].tolist())workbook.save(filename='demo.xlsx')
檔案開啟效果
3.2 Excel讀
實操程式碼
from openpyxl import Workbook, load_workbookfrom openpyxl.workbook.workbook import Worksheetworkbook = load_workbook(filename='demo.xlsx')worksheet = workbook['Sheet']data = []for row in worksheet.iter_rows(min_row=1, max_row=worksheet.max_row, min_col=1, max_col=worksheet.max_column, values_only=True): data.append(row)print(data)
程式碼執行效果
[('Name', 'Age', 'Gender', 'Plans'), ('Michael', 28, 'Male', "I'm studying Python")]
有關檔案讀寫的基礎操作盡在這一份筆記中,小夥伴們可以拿出自己心愛的laptop敲一敲,讓手和大腦一起運動起來哦~
? Be the best version of you
誰要是遊戲人生,他就一事無成;誰不能主宰自己,永遠是一個奴隸。——歌德