In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
Today, I will talk to you about how to use Python to input txt files into Excel forms. Many people may not know much about it. In order to make you understand better, the editor has summarized the following contents for you. I hope you can get something according to this article.
We have a txt file full of data on hand, how to enter it into the Excel table?
Python inputs txt files into Excel
Call xlwt, a function library dedicated to dealing with Excel tables in python, and suggest you open CMD for input.
Pip3 install xlwt
Check to see if this library is installed. Those that are not installed will be installed automatically.
Create a python file, library calls, main functions, custom functions are written first.
Import xlwtdef writeinexcel (): if _ _ name__ = = "_ main__": writeinexcel ()
Open a .txt file
Import xlwtdef writeinexcel (): F = open ('bZhanRank.txt','r',encoding='utf-8') # Open the data text document and note the influence of encoding format if _ _ name__ = = "_ _ main__": writeinexcel ()
Create a new Excel table
Import xlwtdef writeinexcel (): F = open ('bZhanRank.txt','r',encoding='utf-8') # Open the data text document and note the influence of encoding format wb = xlwt.Workbook (encoding='utf-8') # create a new excel file ws1 = wb.add_sheet (' first') # add a new table named first if _ name__ = "_ main__": writeinexcel ()
Do some preprocessing and use the write function
Import xlwtdef writeinexcel (): F = open ('bZhanRank.txt','r',encoding='utf-8') # Open the data text document and note the influence of encoding format wb = xlwt.Workbook (encoding='utf-8') # create a new excel file ws1 = wb.add_sheet (' first') # add a new table The name is first ws1.write (0penol 'name') ws1.write (0pen1 'number of playback') ws1.write (0meme 2 'chase') ws1.write (0mem3 'number of bullet screen') ws1.write ('number of likes') ws1.write (0meme 5) 'coins') row = 1 # start line col = 0 # start column wb.save ("data table .xls") if _ _ name__ = = "_ _ main__": writeinexcel ()
The effect of pretreatment is as follows
Read data from the file (in behavioral units) and write to the Excel table. Don't worry, I have a row of data here with commas (you can also split the data based on spaces or other symbols) so that they can be stored in the corresponding column.
Import xlwtdef writeinexcel (): F = open ('bZhanRank.txt','r',encoding='utf-8') # Open the data text document and note the influence of encoding format wb = xlwt.Workbook (encoding='utf-8') # create a new excel file ws1 = wb.add_sheet (' first') # add a new table The name is first ws1.write (0penol 'name') ws1.write (0pen1 'number of playback') ws1.write (0meme 2 'chase') ws1.write (0mem3 'number of bullet screen') ws1.write ('number of likes') ws1.write (0meme 5) 'coins') row = 1 # start line col = 0 # start column # change pointing to cell position through changes in row and col k = 1 for lines in f: a = lines.split (' ') # the contents of each line in the txt file are separated by commas and stored in an array kappa 1 for i in range (len (a)): ws1.write (row, col, a [I]) # write each item col + = 1 row + = 1 col = 0 wb.save ("data sheet .xls") if _ _ name__ = "_ _ main__": writeinexcel () to the Excel file
Effect picture, do you have your tuition today?
Python gets the content between two keywords between a large piece of text import reclass match3Words (object): lines=0 def _ _ init__ (self,path,word1,word2): self.path = path self.word1 = word1 self.word2 = word2 def key_match (self): with open (self.path) 'rb') as f: buffer = f.read () pattern = re.compile (self.word1+b' (. *?)' + self.word2,re.S) result = pattern.findall (buffer) if result! = []: print (result) # self.lines + = 1 # print ("number of rows matched:" Self.lines) else: print ("keyword not found") path = input ("F:/log.lammps") word1 = "Step Temp TotEng PotEng KinEng Pxx Pyy Pzz Lx Ly Lz v_sxx v_syy v_szz v_sxyxy v_sxzxz v_szyzy" word2 = b "end" matchWords = match3Words (path, word1,word2) matchWords.key_match () read the above Do you have any further understanding of how to use Python to input txt files into Excel forms? If you want to know more knowledge or related content, please follow the industry information channel, thank you for your support.
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.