python程式設計小技巧

2021-09-25 13:14:48 字數 4007 閱讀 6550

#coding:utf-8

import sys

class people():

def __init__(self, name):

print "name:", name

print "*"*30

print "class_name:", self.__class__.__name__

if __name__ == "__main__":

p = people("zhangsan")

執行結果

name: zhangsan

******************************

class_name: people

#coding:utf-8

import sys

def fun_1(name, age):

print "name: %s, age: %s" %(name, age)

def fun_2(school):

print "school: %s" %(school)

def run_fun(fun_name, *arg):

try:

fun_name(*arg)

except exception,e:

print "error\t error_type=%s\t error_info=%s" %(fun_name.__name__, repr(e))

if __name__ == "__main__":

run_fun(fun_1, "zhangsan", 29)

run_fun(fun_2, "henan politecnic university")

run_fun(fun_2, "henan politecnic university", "test")

執行結果

name: zhangsan, age: 29

school: henan politecnic university

error error_type=fun_2 error_info=typeerror('fun_2() takes exactly 1 argument (2 given)',)

str.split(str="", num=string.count(str))
引數:

返回值:返回分割後的字串列表。

示例:

print "hehe nlj 20".split(" ")

print "hehe nlj 20".split(" ", 1)

執行結果

['hehe', 'nlj', '20']

['hehe', 'nlj 20']

示例

#coding:utf-8

import sys

f = open("write.txt", "w")

my_str = "hello world !!!"

#方法1:

f.write(my_str+"\n")

#方法2:

print >> f, my_str

執行結果:

以上兩種方法的效果一樣

hello world !!!

hello world !!!

示例

目錄結構如下:

|—load_conf.py

| ----conf

​ |----test_1.json

​ |----test_2.json

​ |----test_3.cfg

test_1.json

[

"demo1feature",

"demo2feature",

"demo3feature"

]

test_2.json

,

"outlier_model":

}

test_3.cfg

[default]

name="zhangsan"

age="30"

school="henan politecnic university"

load_conf.py**

#coding:utf-8

import json

import glob

import re

import sys

import configparser

def load_json():

featurelib_conf_dir = "conf"

featurelib_conf_suffix_extension = ".json"

for feature_lib_conf_file in glob.glob(featurelib_conf_dir+'/*'+ featurelib_conf_suffix_extension):

with open(feature_lib_conf_file, 'r') as f:

feature_production_conf = json.loads(re.sub('(\/\/.*)', '', re.sub('(/\*(.*?)\*/)', '', f.read(), flags=re.s)))

print feature_lib_conf_file, "*"*30, type(feature_production_conf)

if isinstance(feature_production_conf, list):

for k in feature_production_conf:

print k

elif isinstance(feature_production_conf, dict):

for k,v in feature_production_conf.items():

print k,v

def load_cfg():

conf_dir = "conf"

conf_suffix_extension = ".cfg"

for conf_file in glob.glob(conf_dir+'/*'+conf_suffix_extension):

print "conf_file:", conf_file, "*"*30

cfg = configparser.configparser()

cfg.read(conf_file)

sections = cfg.sections()

for section in sections:

print "section:", section, "+"*20

for k,v in cfg.items(section):

print k,"-->", v

if __name__ == "__main__":

load_json()

load_cfg()

執行結果:

conf/test_1.json ****************************** demo1feature

demo2feature

demo3feature

conf/test_2.json ****************************** outlier_model

ug_model

conf_file: conf/test_3.cfg ******************************

section: default ++++++++++++++++++++

name --> "zhangsan"

age --> "30"

school --> "henan politecnic university"

python 程式設計小技巧

最簡單的方法式利用集合操作 data list 1,1,2,3,4,4 data unique set data list 利用zip函式,打包兩個物件 也可以打包多個物件 l1 1,2,3,4 l2 5,6,7,8 zip l1,l2 生成了乙個新的迭代物件 list zip l1,l2 1,5 ...

Python程式設計小技巧總結

經常看到關於python程式設計的小技巧,但是都沒有記錄下來,當下次運用時還是原來的寫法,這不記錄一下。python 裡有個小彩蛋 在 python shell 裡輸入import this 這段話被稱作 python 之禪 the zen of python 它列舉了一些 python 所推崇的理...

10個Python程式設計小技巧

2a,b b,a 實現了對兩個數的交換 a,b 2,1 name jack country china age 18 1.傳統的字串拼接 很繁雜 print hi,i m name i m from country and i m str age years old.2.百分號語法 print hi...