網(wǎng)站首頁(yè) 編程語(yǔ)言 正文
1.python實(shí)現(xiàn)對(duì)doc文檔的讀取
#讀取docx中的文本代碼示例 import docx #獲取文檔對(duì)象 file=docx.Document("path") print("段落數(shù):"+str(len(file.paragraphs)))#段落數(shù)為13,每個(gè)回車(chē)隔離一段 ? #輸出每一段的內(nèi)容 for para in file.paragraphs: ? ? print(para.text) ? #輸出段落編號(hào)及段落內(nèi)容 for i in range(len(file.paragraphs)): ? ? print("第"+str(i)+"段的內(nèi)容是:"+file.paragraphs[i].text)
2.python實(shí)現(xiàn)對(duì)txt文檔的讀取
filename = 'tangqing.txt' # txt文件和當(dāng)前腳本在同一目錄下,所以不用寫(xiě)具體路徑 pos = [] Efield = [] with open(filename, 'r') as file_to_read: while True: lines = file_to_read.readline() # 整行讀取數(shù)據(jù) if not lines: break p_tmp= [float(i) for i in lines.split()] # 將整行數(shù)據(jù)分割處理,如果分割符是空格,括號(hào)里就不用傳入?yún)?shù),如果是逗號(hào), 則傳入‘,'字符。 pos = np.array(p_tmp) # 將數(shù)據(jù)從list類(lèi)型轉(zhuǎn)換為array類(lèi)型。 print(pos)
3.python實(shí)現(xiàn)對(duì)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)) ? #根據(jù)索引獲取Excel表格中的數(shù)據(jù) ? 參數(shù):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 #行數(shù) ? ? ncols = table.ncols #列數(shù) ? ? colnames = ?table.row_values(colnameindex) #某一行數(shù)據(jù)? ? ? 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 ? #根據(jù)名稱(chēng)獲取Excel表格中的數(shù)據(jù) ? 參數(shù):file:Excel文件路徑 ? ? colnameindex:表頭列名所在行的所以 ?,by_name:Sheet1名稱(chēng) def excel_table_byname(file= 'E:\\個(gè)人文件\\6-desktop\\豐沙點(diǎn)表-配電所.xls',colnameindex=0,by_name=u'電度'): ? ? data = open_excel(file) ? ? table = data.sheet_by_name(by_name) ? ? nrows = table.nrows #行數(shù)? ? ? colnames = ?table.row_values(colnameindex) #某一行數(shù)據(jù)? ? ? 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
相關(guān)推薦
- 2024-03-24 required a single bean, but 2 were found
- 2022-04-23 R語(yǔ)言繪制line?plot線圖示例詳解_R語(yǔ)言
- 2022-08-04 PyTorch中torch.manual_seed()的用法實(shí)例詳解_python
- 2023-07-24 vxe-grid實(shí)現(xiàn) 二維數(shù)據(jù)聯(lián)動(dòng)
- 2022-03-21 oracle中commit之后進(jìn)行數(shù)據(jù)回滾的方法_oracle
- 2022-06-17 go語(yǔ)言beego框架jwt身份認(rèn)證實(shí)現(xiàn)示例_Golang
- 2024-07-18 restTemplate使用總結(jié)
- 2023-10-14 C/C++--跨平臺(tái)--預(yù)定義宏 WIN32、_WIN32、_WIN64
- 最近更新
-
- window11 系統(tǒng)安裝 yarn
- 超詳細(xì)win安裝深度學(xué)習(xí)環(huán)境2025年最新版(
- Linux 中運(yùn)行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲(chǔ)小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎(chǔ)操作-- 運(yùn)算符,流程控制 Flo
- 1. Int 和Integer 的區(qū)別,Jav
- spring @retryable不生效的一種
- Spring Security之認(rèn)證信息的處理
- Spring Security之認(rèn)證過(guò)濾器
- Spring Security概述快速入門(mén)
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權(quán)
- redisson分布式鎖中waittime的設(shè)
- maven:解決release錯(cuò)誤:Artif
- restTemplate使用總結(jié)
- Spring Security之安全異常處理
- MybatisPlus優(yōu)雅實(shí)現(xiàn)加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務(wù)發(fā)現(xiàn)-Nac
- Spring Security之基于HttpR
- Redis 底層數(shù)據(jù)結(jié)構(gòu)-簡(jiǎn)單動(dòng)態(tài)字符串(SD
- arthas操作spring被代理目標(biāo)對(duì)象命令
- Spring中的單例模式應(yīng)用詳解
- 聊聊消息隊(duì)列,發(fā)送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠(yuǎn)程分支