• 【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字符串处理能力很出色;

  • 相关阅读:
    eclipse 10个常用 快捷键
    struts2 s:set标签
    web项目的路径问题
    linux系统中用户切换
    java的多线程(一)
    Mongodb 与 Mongoose 的使用
    escape()、encodeURI()、encodeURIComponent()区别详解
    JSON WEB TOKEN
    关于使用REST API
    mac下设置mongodb开机启动方法
  • 原文地址:https://www.cnblogs.com/caixu/p/3444312.html
Copyright © 2020-2023  润新知