zoukankan      html  css  js  c++  java
  • 【Python】一、除法问题及基本操作(逻辑与,if替代switch)及支持中文打印

    1.查看版本

    C:UsersXXX>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:HKADClogBBP1'
    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字符串处理能力很出色;

  • 相关阅读:
    经典布局 ---- 双飞翼
    细嚼浏览器兼容----条件注释判断浏览器版本
    webqq的注册登记和聊天页面--运用jsonp跨域
    Bootstrap框架的要点--栅格系统
    html5橡皮檫特效
    PHP正确获取客户端IP地址
    常用排序算法及Java实现
    Math类中的floor、ceil和round方法
    Java中的动态反射机制和动态代理
    测试
  • 原文地址:https://www.cnblogs.com/caixu/p/3444312.html
Copyright © 2011-2022 走看看