Python application case and Python application case

Source: Internet
Author: User

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()








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.