Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to read and write doc,txt,xls documents by python

2025-04-11 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

This article introduces the relevant knowledge of "how to read and write doc,txt,xls documents by python". In the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!

Reading doc documents by 1.python

# example of reading text code in docx

Import docx

# get document object

File=docx.Document ("path")

Print ("number of paragraphs:" + str (len (file.paragraphs) # the number of paragraphs is 13, and each carriage return isolates one paragraph

# output the contents of each paragraph

For para in file.paragraphs:

Print (para.text)

# output paragraph number and paragraph content

For i in range (len (file.paragraphs)):

The content of print (paragraph "+ str (I) +") is: "+ file.graphies [I] .text)

Reading txt documents by 2.python

Filename = 'tangqing.txt' # txt file is in the same directory as the current script, so you don't have to write a specific path

Pos = []

Efield = []

With open (filename,'r') as file_to_read:

While True:

Lines = file_to_read.readline () # whole row read data

If not lines:

Break

Pairtmp = [float (I) for i in lines.split ()] # splits the entire line of data. If the separator is a space, there is no parameter in parentheses, and if it is a comma, pass in the', 'character.

Pos = np.array (p_tmp) # converts data from list type to array type.

Print (pos)

Reading xls Table by 3.python

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))

# get the data parameters in the Excel table according to the index: file:Excel file path colnameindex: the index of the row where the header column name is located, and by_index: the index of the table

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 # rows

Ncols = number of table.ncols # columns

Colnames = table.row_values (colnameindex) # A row of data

List = []

For rownum in range (1):

Row = table.row_values (rownum)

If row:

App = {}

For i in range (len (colnames)):

App [colnamesI] = row [I]

List.append (app)

Return list

# get the data parameters in the Excel table according to the name: file:Excel file path colnameindex: the row where the header column name is located, so, by_name:Sheet1 name

Def excel_table_byname (file= 'e:\\ personal file\\ 6-desktop\\ Fengsha point meter-power distribution substation .xls', colnameindex=0,by_name=u' electricity'):

Data = open_excel (file)

Table = data.sheet_by_name (by_name)

Nrows = table.nrows # rows

Colnames = table.row_values (colnameindex) # A row of data

List = []

For rownum in range (1):

Row = table.row_values (rownum)

If row:

App = {}

For i in range (len (colnames)):

App [colnamesI] = 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 ()

This is the end of the content of "how to read and write doc,txt,xls documents by python". Thank you for your reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report