Implementation of the doc document reading
# Read the text in the docx code example import docx # Get document object file=("path") print("Number of paragraphs:"+str(len()))# The number of paragraphs is 13, each carriage return segregates a paragraph # Output the content of each paragraph for para in : print() # Output paragraph numbers and paragraph content for i in range(len()): print("No."+str(i)+"The paragraph reads:"+[i].text)
Implementation of the txt document reading
filename = '' # The txt file is in the same directory as the current script, so you don't need to write a specific path. pos = [] Efield = [] with open(filename, 'r') as file_to_read: while True: lines = file_to_read.readline() # Read data in whole rows if not lines: break p_tmp= [float(i) for i in ()] # Split the whole line of data, if the separator is a space, no parameter is passed in the parentheses, if it is a comma, pass in the ',' character. pos = (p_tmp) # Convert data from a list type to an array type. print(pos)
Implementation of reading xls tables
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 in the Excel table according to the index Parameters: file: Excel file path colnameindex: the index of the row where the table header column name is located , by_index: the index of the table def excel_table_byindex(file= 'path/',colnameindex=0,by_index=0): data = open_excel(file) table = ()[by_index] nrows = # of lines ncols = # of columns colnames = table.row_values(colnameindex) # A line of data 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] (app) return list # According to the name of the Excel table to get the data Parameters: file: Excel file path colnameindex: table header column name of the row so , by_name: Sheet1 name def excel_table_byname(file= 'E:\\\\ personal files\\\\6-desktop\\\\\\\fonsa-pointsheet-distribution-office.xls',colnameindex=0,by_name=u'Electricity'): data = open_excel(file) table = data.sheet_by_name(by_name) nrows = # of lines colnames = table.row_values(colnameindex) # A line of data 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] (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()
to this article on the python implementation of read and write operations on doc, txt, xls document article is introduced to this, more related python document read and write operations neural network data preparation content, please search for my previous posts or continue to browse the following related articles I hope you will support me in the future!