The number of occurrences of each word in the Python statistics text

Source: Internet
Author: User
Tags lua

The number of occurrences of each word in the. Python statistics text: #coding =utf-8__author__ = ' zcg ' Import collectionsimport oswith open (' Abc.txt ') as File1: #打开文本文件 Str1=file1.read (). Split (") #将文章按照空格划分开print" original text: \ n%s "% str1print" \ n the number of occurrences of each word: \ n%s "% collections. Counter (str1) print collections. Counter (STR1) [' A '] #以字典的形式存储, the key value corresponding to each character is the number of occurrences in the text 2.python write generated serialization: __author__ = ' ZCG ' #endcoding utf-8import string, Randomfield=string.letters+string.digitsdef getrandom (): Return "". Join (Random.sample (field,4)) def concatenate ( Group): Return "-". Join ([Getrandom () for I in Range (group)]) def generate (n): Return [Concatenate (4) to I in range (n )]if __name__ = = ' __main__ ': print Generate (10) 3. Iterate through all the data in the Excel table: __author__ = ' Administrator ' Import Xlrdworkbook = xlrd. Open_workbook (' config.xlsx ') print "There is {} sheets in the workbook". Format (workbook.nsheets) for Booksheet in Workbook.sheets (): For Col in Xrange (booksheet.ncols): For row in Xrange (booksheet.nrows): Value=book Sheet.cell (row,col). Value PrinT value where xlrd need Baidu download import this module into Python 4. Organize the data in a table into a format of lua type #coding=utf-8__author__ = ' ZCG ' #2017 9/26import Xlrdfileoutput = open (' Configs.lua ', ' W ') writedata= "[email protected]:zcg\n\n\n" workbook = Xlrd.open_workbook ( ' config.xlsx ') print "There is {} sheets in the workbook". Format (workbook.nsheets) for Booksheet in Workbook.sheets (): W Ritedata = writedata+ ' at ' +booksheet.name+ ' ={\n ' for col in Xrange (booksheet.ncols): For row in xrange (Bookshee T.nrows): value = Booksheet.cell (row,col). value if row ==0:writedata = writedata+ ' \ t         ' + ' ["' +value+ '"] ' + ' = ' + ' {' else:writedata=writedata+ ' "' +str (Booksheet.cell (row,col). Value) + '", ' else:writedata=writedata+ '},\n ' else:writedata=writedata+ '}\n\n ' Else:fileOutput.write ( WriteData) Fileoutput.close ()

  

The number of occurrences of each word in the Python statistics text

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.