python實(shí)現(xiàn)對(duì)doc,txt,xls文檔的讀寫(xiě)操作
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類型轉(zhuǎn)換為array類型。 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ù)名稱獲取Excel表格中的數(shù)據(jù) ? 參數(shù):file:Excel文件路徑 ? ? colnameindex:表頭列名所在行的所以 ?,by_name:Sheet1名稱 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()
到此這篇關(guān)于python實(shí)現(xiàn)對(duì)doc,txt,xls文檔的讀寫(xiě)操作的文章就介紹到這了,更多相關(guān)python文檔讀寫(xiě)操作神經(jīng)網(wǎng)絡(luò)數(shù)據(jù)準(zhǔn)備內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
解決使用pip安裝報(bào)錯(cuò):Microsoft?Visual?C++?14.0?is?required.
對(duì)于程序員來(lái)說(shuō),經(jīng)常pip安裝自己所需要的包,大部分的包基本都能安裝,但是總會(huì)遇到包安裝不了的問(wèn)題,下面這篇文章主要給大家介紹了關(guān)于如何解決使用pip安裝報(bào)錯(cuò):Microsoft?Visual?C++?14.0?is?required.的相關(guān)資料,需要的朋友可以參考下2022-09-09Python3中PyQt5簡(jiǎn)單實(shí)現(xiàn)文件打開(kāi)及保存
本文將結(jié)合實(shí)例代碼,介紹Python3中PyQt5簡(jiǎn)單實(shí)現(xiàn)文件打開(kāi)及保存,具有一定的參考價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2021-06-06Django發(fā)送郵件和itsdangerous模塊的配合使用解析
這篇文章主要介紹了Django發(fā)送郵件和itsdangerous模塊的配合使用解析,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2019-08-08Python中的descriptor描述器簡(jiǎn)明使用指南
descriptor在Python中主要被用來(lái)定義方法和屬性,使用起來(lái)相當(dāng)具有技巧性,這里我們先從基礎(chǔ)的開(kāi)始,整理一份Python中的descriptor描述器簡(jiǎn)明使用指南2016-06-06R vs. Python 數(shù)據(jù)分析中誰(shuí)與爭(zhēng)鋒?
R和Python兩者誰(shuí)更適合數(shù)據(jù)分析領(lǐng)域?在某些特定情況下誰(shuí)會(huì)更有優(yōu)勢(shì)?還是一個(gè)天生在各方面都比另一個(gè)更好?2017-10-10利用Python腳本實(shí)現(xiàn)傳遞參數(shù)的三種方式分享
使用python腳本傳遞參數(shù)在實(shí)際工作過(guò)程中還是比較常用。這篇文章為大家總結(jié)了三個(gè)常用的方式,感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下2022-12-12使用Python通過(guò)QQ郵箱發(fā)送電子郵件的示例代碼
本文介紹如何使用 Python 的 smtplib 和 email 庫(kù)通過(guò) QQ 郵箱發(fā)送電子郵件,文中有詳細(xì)的代碼示例供大家參考,具有一定的參考價(jià)值,需要的朋友可以參考下2023-10-10Python解決pip install時(shí)出現(xiàn)的Could not fetch URL問(wèn)題
這篇文章主要介紹了Python解決pip install時(shí)出現(xiàn)的Could not fetch URL問(wèn)題,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2019-08-08