• python的N个小功能(更新文件)


    ##########################################################################

    #对于第二份文件:第一份与第二份不相同,以第二份为主;

    #第一份存在,第二份不存在,修改为0;第一份不存在,第二份存在,以第二份为主

    ##########################################################################

    # -*- coding: utf-8 -*-

    """

    Created on Wed Feb 22 13:40:03 2017

    @author: Administrator

    """

    import sys

    def readFile(filename):   ##读文件

        re = {}

        for line in open(filename):

            arr = line.strip().split(' ')  #  s.strip(rm) 删除s字符串中开头、结尾处,位于 rm删除序列的字符;

                                           #1. 当rm为空时,默认删除空白符(包括' ', ' ',  ' ',  ' ')

                                           #2.这里的rm删除序列是只要边(开头或结尾)上的字符在删除序列内,就删除掉

            if len(arr)<=2:

                continue

            key = arr[0] + '_' + arr[1]

            re.setdefault(key, arr[2])   #dict.setdefault(key, default=None),

                                        #key——查找的键值;default——键不存在时,设置的默认键值  

        return re

    def Minus(baseFile, otherFile):       #对于第二份文件:第一份与第二份不相同,以第二份为主;

                                          #第一份存在,第二份不存在,修改为0;第一份不存在,第二份存在,以第二份为主

        re = {}

        baseDict = readFile(baseFile)

        otherDict = readFile(otherFile)

        for k,v in baseDict.items():

            if k in otherDict:

                if not baseDict[k].isdigit() or not otherDict[k].isdigit():

                         continue

                c = int(baseDict[k]) - int(otherDict[k])                       

                if c != 0:   

                    re[k] = otherDict[k]

                     continue

            if k not in otherDict:

                if not baseDict[k].isdigit() :

                    continue

                c = 0

                re[k] = str(c)

                continue

        for k,v in otherDict.items():

            if k not in baseDict:

                if not otherDict[k].isdigit():

                    continue

                re[k] = otherDict[k]  

        return re

    def writeFile(baseDict):

        for k,v in baseDict.items():   #dict = { 1 : 2, 'a' : 'b', 'hello' : 'world' }

                 #dict.items()  [('a', 'b'), (1, 2), ('hello', 'world')]

            arr = k.split('_')

                 print "%s %s %s" % (arr[0], arr[1], v)

    def main():

        #baseFile = sys.argv[1]

        #otherFile = sys.argv[2]

        baseFile = r'G:pythoncodeone.txt'

        otherFile = r'G:pythoncode wo.txt'

        baseDict = Minus(baseFile, otherFile)

        writeFile(baseDict)

    if __name__=='__main__':

        main()

                      

    #####################################################

    ######################案例###########################

    one.txt

    1   2   3

    4   5   6

    7   8   9

    10  11  12

    two.txt

    1  2   3

    7   8   9

    10  11  8

    13  14  15

    输出

    13  14  15

    10  11  8

    4   5   0               

  • 相关阅读:
    Linux(Ubuntu)使用日记------ssh远程登录腾讯云
    Linux(Ubuntu)使用日记------vim复制内容到其他应用
    Linux(Ubuntu)使用日记(七)------终端控制器Terminator安装使用
    Error updating database. Cause: java.sql.SQLException: Access denied for user '${username}'@'localhost' (using password: YES)
    Linux(Ubuntu)使用日记(三)------git安装使用
    90分钟实现一门编程语言——极简解释器教程
    新人入职100天,聊聊自己的经验&教训
    2010~2013,三年三十万+,从挣钱角度回忆自己研究生三年时光
    flex布局总结
    react项目 之 server-mongod数据库框架
  • 原文地址:https://www.cnblogs.com/dudumiaomiao/p/6431166.html
Copyright © 2020-2023  润新知