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

  • 相关阅读:
    JavaWeb之Ajax快速入门(十九)
    JavaWeb之文件上传与下载&邮件技术(十八)
    JavaWeb之Filter快速入门(十七)
    JavaWeb之JDBC分页/监听器(十六)
    JavaWeb之JDBC&自定义框架/DBUtil(十五)
    JavaWeb之JDBC(十四)
    JavaWeb之JDBC学习(十三)
    JavaWeb之Mysql代码练习(十二)
    [ 校内OJ ] NOIP2019模拟赛(五)
    【一本通】欧拉回路
  • 原文地址:https://www.cnblogs.com/caixu/p/3444312.html
Copyright © 2011-2022 走看看