Small case sharing for Python applications

Source: Internet
Author: User
number of occurrences of each word in 1.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
__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) for I in range (n)]if __name__ = = ' __main__ ':    print generate (10)

3. Traverse 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 (): FO R col in Xrange (booksheet.ncols): As row in Xrange (booksheet.nrows): Value=booksheet.cell (row,col). Valu e print value where xlrd need Baidu download import this module into Python 
 #coding =utf-8__author__ = ' ZCG ' #2017 9/26import xlrdfileoutput = open (' Configs.lua ', ' W ') writedata= "--@author: 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 (): WriteData = writedata+ ' at ' +bookshe Et.name+ ' ={\n ' for col in Xrange (booksheet.ncols): For row in Xrange (booksheet.nrows): value = books            Heet.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 () 
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.