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

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

#第一份存在,第二份不存在,修改为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('\t')  #  s.strip(rm) 删除s字符串中开头、结尾处,位于 rm删除序列的字符;

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

#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\t%s\t%s" % (arr[0], arr[1], v)

def main():

#baseFile = sys.argv[1]

#otherFile = sys.argv[2]

baseFile = r'G:\pythoncode\one.txt'

otherFile = r'G:\pythoncode\two.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

python的N个小功能(更新文件)的更多相关文章

  1. python的N个小功能(找到符合要求的图片,重命名,改格式,缩放,进行随机分配)

    ########################################################################## 循环读取该目录下所有子目录和子文件 ####### ...

  2. python的N个小功能(文件内容的匹配替换)

    # -*- coding: utf-8 -*- """ Created on Fri Feb 17 20:25:05 2017 @author: who "&q ...

  3. 【Python】猜数小游戏(文件操作)

    人生苦短,我用Python 关键词 1.多用户 2.字典记录所有成绩 3.每次游戏轮数&总游戏次数&平均每次游戏需要多少轮 字典Dictionary.列表List.元组Tuple差异化 ...

  4. python的N个小功能(找到要爬取的验证码链接,并大量下载验证码样本)

    # -*- coding: utf-8 -*- """ Created on Mon Mar 21 11:04:54 2017 @author: sl "&qu ...

  5. python的N个小功能(连接数据库并下载相应位置的图片)

    #################################################################################################### ...

  6. python的N个小功能之正则匹配

    1.. 匹配任意除换行符“\n”外的字符:2.*表示匹配前一个字符0次或无限次:3.+或*后跟?表示非贪婪匹配,即尽可能少的匹配,如*?重复任意次,但尽可能少重复,惰性匹配:4. .*? 表示匹配任意 ...

  7. python的N个小功能(图片预处理:打开图片,滤波器,增强,灰度图转换,去噪,二值化,切割,保存)

    ############################################################################################# ###### ...

  8. python的N个小功能(高斯模糊原理及实践)

    原理: 二维高斯函数 1)         为了计算权重矩阵,需要设定σ的值.假定σ=1.5,则模糊半径为1的权重矩阵如下: 2)         这9个点的权重总和等于0.4787147,如果只计算 ...

  9. python的N个小功能(文本字段对应数值,经纬度计算距离,两个时间点计算时间间隔)

    案例1 >>> import pandas as pd >>> df=pd.DataFrame({'A':[1,2,3],'B':[1,2,3],'C':[1,2, ...

随机推荐

  1. 20155319 《Java程序设计》实验五(网络编程与安全)实验报告

    20155319 <Java程序设计>实验五(网络编程与安全)实验报告 一.实验内容及步骤 (一) 两人一组结对编程 参考http://www.cnblogs.com/rocedu/p/6 ...

  2. 1126: [POI2008]Uci

    1126: [POI2008]Uci https://lydsy.com/JudgeOnline/problem.php?id=1126 分析: dp.状态很妙,就是有点难写. 能走的是一个矩形.首先 ...

  3. UTC时间转为正常日期

    SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'", Locale.US);SimpleDa ...

  4. 探究linux设备驱动模型之——platform虚拟总线(二)

    上回说到,platform_match是驱动和设备之间的媒人婆,那么platform_match是如何匹配驱动和设备的呢?platform总线定义的匹配条件很简单,主要就是查看驱动结构体和设备结构体的 ...

  5. Smokeping配置

    参考文档: 官网:http://oss.oetiker.ch/smokeping/ 参考:http://jaminzhang.github.io/monitoring/smokeping-deploy ...

  6. 特征点检测--基于CNN:TILDE: A Temporally Invariant Learned DEtector

    TILDE: A Temporally Invariant Learned DEtector Yannick Verdie1,∗ Kwang Moo Yi1,∗ Pascal Fua1 Vincent ...

  7. Linux sync命令的作用分析

    Sync命令   在用reboot命令启动unix系统后,系统提示出错信息,部分应用程序不能正常工作.经仔细检查系统文件,并和初始的正确备份进行比较,发现某些文件确实被破坏了,翻来覆去找不到文件遭破坏 ...

  8. BZOJ 3489 A simple rmq problem 可持久化KDtree/二维线段树

    题目链接:https://www.lydsy.com/JudgeOnline/problem.php?id=3489 题意概述: 给出一个序列,每次询问一个序列区间中仅出现了一次的数字最大是多少,如果 ...

  9. Live Archive 训练题 2019/3/9

    7454 Parentheses A bracket is a punctuation mark, which is used in matched pairs, usually used withi ...

  10. LeetCode 36. Valid Sudoku (C++)

    题目: Determine if a 9x9 Sudoku board is valid. Only the filled cells need to be validated according t ...