Python application case and Python application case
1. python counts the number of times each word appears in the text:
# Coding = UTF-8
_ Author _ = 'zcg'
Import collections
Import OS
With open('abc.txt ') as file1: # open a text file
Str1 = file1.read (). split ('') # divide the article by Space
Print "original text: \ n % s" % str1
Print "\ n the number of times each word appears: \ n % s" % collections. Counter (str1)
Print collections. Counter (str1) ['a'] # stored in dictionary form. The key value corresponding to each character is the number of times that appear in the text
2. compile and generate serialization in python:
__author__ = 'zcg'
#endcoding utf-8
import string,random
field=string.letters+string.digits
def 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 data in an excel table:
_ Author _ = 'admin'
Import xlrd
Workbook = xlrd.open_workbook('config.xlsx ')
Print "There are {} 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 = booksheet. cell (row, col). value
Print value
Xlrd needs Baidu to download and import this module to python.
4. Organize the data in the table into a lua format.
#coding=utf-8
__author__ = 'zcg'
#2017 9/26
import xlrd
fileOutput = open('Configs.lua','w')
writeData="--@author:zcg\n\n\n"
workbook = xlrd.open_workbook('config.xlsx')
print "There are {} sheets in the workbook".format(workbook.nsheets)
for booksheet in workbook.sheets():
writeData = writeData+'AT' +booksheet.name+' ={\n'
for col in xrange(booksheet.ncols):
for row in xrange(booksheet.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()