網站首頁 編程語言 正文
1.python實現對doc文檔的讀取
#讀取docx中的文本代碼示例 import docx #獲取文檔對象 file=docx.Document("path") print("段落數:"+str(len(file.paragraphs)))#段落數為13,每個回車隔離一段 ? #輸出每一段的內容 for para in file.paragraphs: ? ? print(para.text) ? #輸出段落編號及段落內容 for i in range(len(file.paragraphs)): ? ? print("第"+str(i)+"段的內容是:"+file.paragraphs[i].text)
2.python實現對txt文檔的讀取
filename = 'tangqing.txt' # txt文件和當前腳本在同一目錄下,所以不用寫具體路徑 pos = [] Efield = [] with open(filename, 'r') as file_to_read: while True: lines = file_to_read.readline() # 整行讀取數據 if not lines: break p_tmp= [float(i) for i in lines.split()] # 將整行數據分割處理,如果分割符是空格,括號里就不用傳入參數,如果是逗號, 則傳入‘,'字符。 pos = np.array(p_tmp) # 將數據從list類型轉換為array類型。 print(pos)
3.python實現對xls表格的讀取
import ?xdrlib ,sys import xlrd def open_excel(file= 'path'): ? ? try: ? ? ? ? data = xlrd.open_workbook(file) ? ? ? ? return data ? ? except Exception as e: ? ? ? ? print(str(e)) ? #根據索引獲取Excel表格中的數據 ? 參數:file:Excel文件路徑 ? ? colnameindex:表頭列名所在行的索引 ?,by_index:表的索引 def excel_table_byindex(file= 'path/xxx.xls',colnameindex=0,by_index=0): ? ? data = open_excel(file) ? ? table = data.sheets()[by_index] ? ? nrows = table.nrows #行數 ? ? ncols = table.ncols #列數 ? ? colnames = ?table.row_values(colnameindex) #某一行數據? ? ? list =[] ? ? for rownum in range(1,nrows): ? ? ? ? ?row = table.row_values(rownum) ? ? ? ? ?if row: ? ? ? ? ? ? ?app = {} ? ? ? ? ? ? ?for i in range(len(colnames)): ? ? ? ? ? ? ? ? app[colnames[i]] = row[i]? ? ? ? ? ? ? ?list.append(app) ? ? return list ? #根據名稱獲取Excel表格中的數據 ? 參數:file:Excel文件路徑 ? ? colnameindex:表頭列名所在行的所以 ?,by_name:Sheet1名稱 def excel_table_byname(file= 'E:\\個人文件\\6-desktop\\豐沙點表-配電所.xls',colnameindex=0,by_name=u'電度'): ? ? data = open_excel(file) ? ? table = data.sheet_by_name(by_name) ? ? nrows = table.nrows #行數? ? ? colnames = ?table.row_values(colnameindex) #某一行數據? ? ? list =[] ? ? for rownum in range(1,nrows): ? ? ? ? ?row = table.row_values(rownum) ? ? ? ? ?if row: ? ? ? ? ? ? ?app = {} ? ? ? ? ? ? ?for i in range(len(colnames)): ? ? ? ? ? ? ? ? app[colnames[i]] = row[i] ? ? ? ? ? ? ?list.append(app) ? ? return list ? def main(): ? ?tables = excel_table_byindex() ? ?for row in tables: ? ? ? ?print(row) ? ? ? ? ? ? ? ? ?tables = excel_table_byname() ? ?for row in tables: ? ? ? ?print(row) ? ? ? ? ? ? ? if __name__=="__main__": ? ? main()
原文鏈接:https://blog.csdn.net/weixin_45564943/article/details/122597099
相關推薦
- 2022-09-09 Redis中ZSet的具體使用_Redis
- 2022-04-01 SQL?Server?數據庫的設計詳解_MsSql
- 2022-09-01 Nginx?部署的虛擬主機使用?Let's?Encrypt?加密?https的方法_nginx
- 2021-12-18 C/C++?Qt?數據庫與TreeView組件綁定詳解_C 語言
- 2022-06-30 PyTorch實現卷積神經網絡的搭建詳解_python
- 2022-07-12 hadoop 在jps中找不到namenode的原因
- 2022-06-15 GO文件創建及讀寫操作示例詳解_Golang
- 2022-08-25 windows下搭建Consul集群_云其它
- 最近更新
-
- window11 系統安裝 yarn
- 超詳細win安裝深度學習環境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權
- redisson分布式鎖中waittime的設
- maven:解決release錯誤:Artif
- restTemplate使用總結
- Spring Security之安全異常處理
- MybatisPlus優雅實現加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務發現-Nac
- Spring Security之基于HttpR
- Redis 底層數據結構-簡單動態字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應用詳解
- 聊聊消息隊列,發送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支