Python论坛  - 讨论区

标题:[python-chinese] 现在开始写python小程序, 感觉还没习惯, 问几个问� =E2?=

2005年05月27日 星期五 08:08

泰传 温 wendwghit at yahoo.com.cn
Fri May 27 08:08:29 HKT 2005

1. 正则表达式[.]无法匹配中文, 这是问题码?

import re
>>> m = re.match("[.]*", ";测试")
>>> print m
<_sre.SRE_Match object at 0x008E4368>
>>> m.group()
''

2. 如何直接使用python来实现复杂的数据结构,如树, 现在在写个小应用程序时, 需要建立一颗树, 以用来统计建立文件系统时目录的空间大小, 请问那里有相关的库, 或样例代码, 我是从C过来的, 所以使用python还不是很习惯, 看看下面的代码就知道了, 帮忙指点有那些是可以使用python来进一步改进

"""
    Check Script Tools.
    用来检验Script的正确性, 也用于正确设置统计目录空间的大小
"""
import os
import copy
import re
import string

# 去掉一些无效的串
def tripLine(srcStr):
    """
        srcList is String, if should trip
    """
    srcStr = string.upper(srcStr)
    
    if re.match("^[ \t\n]*$", srcStr):
        return ""  # 空行

    m = re.match("^[ \t]*[^;]*;", srcStr)
    if m:       # 批处理中的注释, 这里只处理使用;开头的注释
        srcStr = m.group()[0 : -1] # 去掉剩下的;

    m = re.match("[^\(]*\(", srcStr)
    if m:
        srcStr = m.group()[0 : -1]  # 去掉 状态字

    m = re.findall("[\d\w]+", srcStr)
    srcStr = ""
    for x in m:
        srcStr = srcStr + x       # 去掉串中的空格和\t

    return srcStr


# 将文件内容转换为一个串的列表
def fileToList(destList, fileName):
    """
        Read file and translate to lines.
    """
    try:
        fp = open(fileName, "r")
    except IOError:
        print "Cannot Open FIle ", fileName
        raise IOError
    st = []
    readStr = ""
    lineNum = int(1)
    readStr = fp.readline()
    st = [lineNum, readStr]
    while readStr:
        st[0] = lineNum
        st[1] = tripLine(readStr)
        if st[1]:            
            destList.append(copy.deepcopy(st))
        else:
            pass
        lineNum = lineNum + 1
        readStr = fp.readline()
 
    fp.close()
    return

# 错误信息串
__staticErrorStr = (    "Not ERROR",                    # 0x00
                        "P3 ERROR",                     # 0x01
                        "APDU Too Short",               # 0x02
                        "Data Length is not Even",      # 0x03
                        "CLA ERROR, must A0 or C0",     # 0x04
                        "INS ERROR"                     # 0x05
                    )

# 打印错误信息
def __printErrorMsg(errorNum, lNum):
    global __staticErrorStr
    print "Incorrect APDU, Line %d\tReason: %s" %(lNum, __staticErrorStr[errorNum])

def __isCorrectCLA(CLA, lNum):
    if (CLA == "A0") or (CLA == "C0"):
        return
    else:
        __printErrorMsg(0x04, lNum)

def __isCorrectINS(INS, lNum):
    if 1:
        return
    else:
        __printErrorMsg(0x05, lNum)

# 十六进制数值转换用表
__staticHexDecMap = {   "0": 0x00, "1": 0x01, "2": 0x02, "3": 0x03, "4": 0x04, "5": 0x05, "6": 0x06, "7": 0x07, "8": 0x08,
                        "9": 0x09, "A": 0x0A, "a": 0x0A, "B": 0x0B, "b": 0x0B, "C": 0x0C, "c": 0x0C, "D": 0x0D, "d": 0x0D,
                        "E": 0x0E, "e": 0x0E, "F": 0x0F, "f": 0x0F
                    }
def __hexStrToDec(hexStr):
    global __staticHexDecMap
    r = 0L
    for x in hexStr:
        if x in string.hexdigits:
            r = r * 16 + __staticHexDecMap[x]
        else:
            return 0L

    return r

# 校验每行批处理的参数有效性
def isLineParamError(cmdLine):
    lineNum = cmdLine[0]
    cmdData = cmdLine[1]
    dataLen = len(cmdData)

    if (dataLen % 2) != 0:
        __printErrorMsg(0x03, lineNum)
        return 0x01

    dataLen = dataLen / 2
    if dataLen < 5:
        __printErrorMsg(0x02, lineNum)
        return 0x01

    CLA = cmdData[0:2]
    INS = cmdData[2:4]
    P3 = cmdData[8:10]

    __isCorrectCLA(CLA, lineNum)
    __isCorrectINS(INS, lineNum)

    if (len(cmdData[10 : len(cmdData)]) / 2) != __hexStrToDec(P3):
        __printErrorMsg(0x01, lineNum)
        return 0x01

    return 0x00


def checkLineParamError(destList):
    error = 0x00
    tmp = 0x00
    for x in destList:
        tmp = isLineParamError(x)
        if error == 0x00:
            error = tmp

    return error

def main():
    list = []
    fName = os.path.join(os.getcwd(), "Script.cmd")
    fileToList(list, fName)

    if checkLineParamError(list):
        return       
    
    for x in list:
        print x[0], x[1]

if __name__ == "__main__":
    main()



---------------------------------
Do You Yahoo!?
150万曲MP3疯狂搜,带您闯入音乐殿堂
美女明星应有尽有,搜遍美图、艳图和酷图
1G就是1000兆,雅虎电邮自助扩容!
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.exoweb.net/pipermail/python-chinese/attachments/20050527/f5ee0d90/attachment.html

[导入自Mailman归档:http://www.zeuux.org/pipermail/zeuux-python]

2005年05月27日 星期五 08:56

Zoom Quiet zoom.quiet at gmail.com
Fri May 27 08:56:01 HKT 2005

就我个人经验,中文的正则表达式匹配好象是不行

复杂的数据结构??
字典,列表,元组,本来就可以混合使用的,足够折腾的,
不过从分析角度,直接记录到XML不是更好?
http://wiki.woodpecker.org.cn/moin/CvsWeeklyStat
就是类似的分析应用,不过提示一点在Unix 环境中有N多更加精巧的工具,使用Python 来调用它们吧!不一定非要纯Py 的哪?

在 05-5-27,泰传 温<wendwghit at yahoo.com.cn> 写道:
> 
> 
> 1. 正则表达式[.]无法匹配中文, 这是问题码? 
> 
> import re
> >>> m = re.match("[.]*", ";测试")
> >>> print m
> <_sre.SRE_Match object at 0x008E4368>
> >>> m.group()
> '' 
> 
> 2. 如何直接使用python来实现复杂的数据结构,如树, 现在在写个小应用程序时, 需要建立一颗树, 以用来统计建立文件系统时目录的空间大小,
> 请问那里有相关的库, 或样例代码, 我是从C过来的, 所以使用python还不是很习惯, 看看下面的代码就知道了,
> 帮忙指点有那些是可以使用python来进一步改进 
> 
> """
>     Check Script Tools.
>     用来检验Script的正确性, 也用于正确设置统计目录空间的大小
> """
> import os
> import copy
> import re
> import string 
> 
> # 去掉一些无效的串
> def tripLine(srcStr):
>     """
>         srcList is String, if should trip
>     """
>     srcStr = string.upper(srcStr)
>     
>     if re.match("^[ \t\n]*$", srcStr):
>         return ""  # 空行 
> 
>     m = re.match("^[ \t]*[^;]*;", srcStr)
>     if m:       # 批处理中的注释, 这里只处理使用;开头的注释
>         srcStr = m.group()[0 : -1] # 去掉剩下的; 
> 
>     m = re.match("[^\(]*\(", srcStr)
>     if m:
>         srcStr = m.group()[0 : -1]  # 去掉 状态字 
> 
>     m = re.findall("[\d\w]+", srcStr)
>     srcStr = ""
>     for x in m:
>         srcStr = srcStr + x       # 去掉串中的空格和\t 
> 
>     return srcStr 
> 
> 
> # 将文件内容转换为一个串的列表
> def fileToList(destList, fileName):
>     """
>         Read file and translate to lines.
>     """
>     try:
>         fp = open(fileName, "r")
>     except IOError:
>         print "Cannot Open FIle ", fileName
>         raise IOError
>     st = []
>     readStr = ""
>     lineNum = int(1)
>     readStr = fp.readline()
>     st = [lineNum, readStr]
>     while readStr:
>         st[0] = lineNum
>         st[1] = tripLine(readStr)
>         if st[1]:            
>             destList.append(copy.deepcopy(st))
>         else:
>             pass
>         lineNum = lineNum + 1
>         readStr = fp.readline()
>  
>     fp.close()
>     return 
> 
> # 错误信息串
> __staticErrorStr = (    "Not ERROR",                    # 0x00
>                         "P3 ERROR",                     # 0x01
>                         "APDU Too Short",               # 0x02
>                         "Data Length is not Even",      # 0x03
>                         "CLA ERROR, must A0 or C0",     # 0x04
>                         "INS ERROR"                     # 0x05
>                     ) 
> 
> # 打印错误信息
> def __printErrorMsg(errorNum, lNum):
>     global __staticErrorStr
>     print "Incorrect APDU, Line %d\tReason: %s" %(lNum,
> __staticErrorStr[errorNum]) 
> 
> def __isCorrectCLA(CLA, lNum):
>     if (CLA == "A0") or (CLA == "C0"):
>         return
>     else:
>         __printErrorMsg(0x04, lNum) 
> 
> def __isCorrectINS(INS, lNum):
>     if 1:
>         return
>     else:
>         __printErrorMsg(0x05, lNum) 
> 
> # 十六进制数值转换用表
> __staticHexDecMap = {   "0": 0x00, "1": 0x01, "2": 0x02, "3": 0x03, "4":
> 0x04, "5": 0x05, "6": 0x06, "7": 0x07, "8": 0x08,
>                         "9": 0x09, "A": 0x0A, "a": 0x0A, "B": 0x0B, "b":
> 0x0B, "C": 0x0C, "c": 0x0C, "D": 0x0D, "d": 0x0D,
>                         "E": 0x0E, "e": 0x0E, "F": 0x0F, "f": 0x0F
>                     }
> def __hexStrToDec(hexStr):
>     global __staticHexDecMap
>     r = 0L
>     for x in hexStr:
>         if x in string.hexdigits:
>             r = r * 16 + __staticHexDecMap[x]
>         else:
>             return 0L 
> 
>     return r 
> 
> # 校验每行批处理的参数有效性
> def isLineParamError(cmdLine):
>     lineNum = cmdLine[0]
>     cmdData = cmdLine[1]
>     dataLen = len(cmdData) 
> 
>     if (dataLen % 2) != 0:
>         __printErrorMsg(0x03, lineNum)
>         return 0x01 
> 
>     dataLen = dataLen / 2
>     if dataLen < 5:
>         __printErrorMsg(0x02, lineNum)
>         return 0x01 
> 
>     CLA = cmdData[0:2]
>     INS = cmdData[2:4]
>     P3 = cmdData[8:10] 
> 
>     __isCorrectCLA(CLA, lineNum)
>     __isCorrectINS(INS, lineNum) 
> 
>     if (len(cmdData[10 : len(cmdData)]) / 2) != __hexStrToDec(P3):
>         __printErrorMsg(0x01, lineNum)
>         return 0x01 
> 
>     return 0x00 
> 
> 
> def checkLineParamError(destList):
>     error = 0x00
>     tmp = 0x00
>     for x in destList:
>         tmp = isLineParamError(x)
>         if error == 0x00:
>             error = tmp 
> 
>     return error 
> 
> def main():
>     list = []
>     fName = os.path.join(os.getcwd(), "Script.cmd")
>     fileToList(list, fName) 
> 
>     if checkLineParamError(list):
>         return       
>     
>     for x in list:
>         print x[0], x[1] 
> 
> if __name__ == "__main__":
>     main()
> 
> 
> ________________________________
> Do You Yahoo!?
>  150万曲MP3疯狂搜,带您闯入音乐殿堂
> 美女明星应有尽有,搜遍美图、艳图和酷图
>  1G就是1000兆,雅虎电邮自助扩容! 
> 
> 
> _______________________________________________
> python-chinese list
> python-chinese at lists.python.cn
> http://python.cn/mailman/listinfo/python-chinese
> 
> 
> 


-- 
[Time is unimportant, only life important!]

[导入自Mailman归档:http://www.zeuux.org/pipermail/zeuux-python]

如下红色区域有误,请重新填写。

    你的回复:

    请 登录 后回复。还没有在Zeuux哲思注册吗?现在 注册 !

    Zeuux © 2025

    京ICP备05028076号