In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-31 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)05/31 Report--
This article mainly introduces "how Python integrates multiple sheet tables". In daily operation, I believe many people have doubts about how Python integrates multiple sheet tables. Xiaobian consulted all kinds of data and sorted out simple and easy-to-use methods of operation. I hope it will be helpful to answer the doubts of "how Python integrates multiple sheet tables". Next, please follow the editor to study!
Description
1. The xlwt module is not appended to the .xls module, so write for loops and lists at one time, so there is no appending or non-appendage.
2, merge the Excel table, merge each Excel table as rows, that is, merge rows, change the idea, merge the tags in the Excel table as columns, that is, merge different tags composed of the same tags in different files, merge the same tags in different files first, merge the same tags in different files to form a list, and then merge the different tags formed before, you can get the contents of all Excel documents.
Example
# Import xlrd and xlwt module # xlrd module is the module that reads the Excel file of .xls, and xlwt module is the module that writes the Excel file of .xls in a non-appended manner. Import xlrd,xlwt# imports the path of the file to be read a = ["C:/Users/Desktop/m1.xls", "C:/Users/Desktop/m2.xls"] # defines the path of the file to be written b = "C:/Users/Desktop/m3.xls" # defines the function Determine whether an exception will occur when opening a file def open (fileaddress): try:# opens an Excel file through the open_workbook () method of the xlrd module, and defines the variable fo as the file handle fo=xlrd.open_workbook (fileaddress) return fo except Exception as e: print ("error!" + str (e)) # defines the function Get the number of lines of a tag in a certain Excel file def getrows (fo,num): table=fo.sheets () [num] # sheets () function is the function included in the xlrd module, and the tag n=table.nrows#nrows that can be returned to the Excel file in the form of a list is the function included in the xlrd module. Through a tag call, you can get the number of lines of the tag return n "" definition function. You can get the content of any tag in any Excel file through the sequence number of the path and tag of the Excel file, and return "def getsheetcontents (fileaddress,num): avalue= [] fo=open (fileaddress) table=fo.sheets () [num] n=table.nrows for i in range (0Lind n):" row_values () function is a function included in the xlrd module, which is called through a tag. The row data of the tag can be obtained, and the row data is returned as a list. The tag is listed as a list element "" rdate=table.row_values (I) avalue.append (rdate) return avaluesvalue= [] k = [] # get the tag number of all Excel files through the for loop And return for i in an as a list: fo=open (I) k.append (len (fo.sheets () # sort these tags in ascending order k.sort () # put all the contents of different tags in these Excel files in the list svalue through the for loop for i in range (0 K [len (k)-1]): # put the same tag of different Excel files in the list bvalue through the for loop bvalue= [] for j in a: print ("reading" + str (I) + "tag" of "+ str (j) +") bvalue.append (getsheetcontents (j) I) svalue.append (bvalue) # svalue contains the contents of all Excel files # create a .xls file through the Workbook () of the xlwt module, and define a variable fw as the file handle fw=xlwt.Workbook () line=0#add_sheet () function for the xlwt module with its own function You can get a custom tag # define the variable ws as the tag handle ws=fw.add_sheet ("sheet1") "iterate through the svalue list through the for loop, and write the data of the resulting tag, a file, a row and a column into the new tag" for i in range (0Len (svalue): for j in range (0Len (Svalue [I])): for m in range (0). Len (Svalue [I] [j]): for n in range (0Len (Svalue [I] [j] [m])): ws.write (line,n,svalue [I] [j] [m] [n]) # the increasing number of rows can be obtained through line line+=1print ("merge complete") # save () function is the xlwt built-in function Save the merged Excel file to a certain path fw.save (b) # xlrd module and xlwt module have no close () function, that is, use these two modules to open the file without closing the file here, on the "Python how to integrate multiple sheet tables" the study is over, I hope to be able to solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!
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.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.