【Python】一、除法问题及基本操作(逻辑与,if替代switch)及支持中文打印

1.查看版本

C:\Users\XXX>python -V
Python 2.7.1

2.除法问题(不要整除)

from __future__ import division
tmp=0x3ec099-0x389341
#res=((tmp & 0x3ff)*2500)>>10
print tmp*31/1000

3.读文件操作 & 两张替换方法 & for循环 & 字符转换数字 & 比较 & 正则表达式 & 转义字符\

题目:对指定文件的内容进行裁剪后,取出字符串中最大最小的数字

import os
import re
path= 'D:\HKADC\log\BBP1'
srcfile='a0.txt' min=99999
max=0
#one way to replase
dir=path.replace('\\','/')
os.chdir(dir)
fin=open(srcfile); #line=fin.readline()
for line in fin.readlines():
#other way to replace
pattern1=re.compile('.*-adc:')
res1=pattern1.sub('',line)
#print res1
pattern2=re.compile('-ct:.*')
res=pattern2.sub('',res1)
#if len(res) > 4:
# print res
# continue
if (80<int(res)<105):
print line
if int(res)<min:
min=int(res)
if int(res)>max:
max=int(res)
print min
print max

4.逻辑与用and,由于没有switch,故用if elif替代

if ((master_12_9 == 6) and (Target_SubRange_8_5 == 0)):
BassAddr=0xfd000000
elif ((master_12_9 == 11) and (Target_SubRange_8_5 == 8)):
BassAddr=0x0
else :
print "计算基地址出现异常"

5.需要支持中文打印,在开头请用

#!/usr/bin/python
#-*- encoding:UTF-8 -*-
XXXXXX

注:

脚本语言魅力无限;

python字符串处理能力很出色;

上一篇:词法分析程序的设计与实现10.11


下一篇:【POJ 3487】 The Stable Marriage Problem (稳定婚姻问题)