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

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

#第一份存在,第二份不存在,修改为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. 20155338 2016-2017-2 《Java程序设计》第九周学习总结

    20155338 2016-2017-2 <Java程序设计>第九周学习总结 教材学习内容总结 第十六章 整合数据库 JDBC入门 · 数据库本身是个独立运行的应用程序 · 撰写应用程序是 ...

  2. sql语句-2-字符串数字日期时间

  3. (三)SpringBoot2.0基础篇- 持久层,jdbcTemplate和JpaRespository

    一.介绍 SpringBoot框架为使用SQL数据库提供了广泛的支持,从使用JdbcTemplate的直接JDBC访问到完整的“对象关系映射”技术(如Hibernate).Spring-data-jp ...

  4. Oracle dba权限下修改用户密码 授予用户权限 解锁用户

    1.修改用户密码 alter user scott identified by 123 2.授予用户权限 grant connect,resource to scott 3.解锁用户 alter us ...

  5. 【Java】 秒转时分秒天

    总有时候会有些需求, 需要用到秒, 比如 JedisCluster 设置过期时间 现在有一个需求是 : 查询接口的缓存设置有效期为:1天+随机时间 基本可以按以下来做: package com.lwc ...

  6. angular之$broadcast、$emit、$on传值

    文件层级 index.html <!DOCTYPE html> <html ng-app="nickApp"> <head> <meta ...

  7. 408. Add Binary【LintCode java】

    Description Given two binary strings, return their sum (also a binary string). Example a = 11 b = 1 ...

  8. Zookeeper--java操作zookeeper

    如果是使用java操作zookeeper,zookeeper的javaclient 使我们更轻松的去对zookeeper进行各种操作,我们引入zookeeper-3.4.5.jar 和 zkclien ...

  9. python基础-02-while格式化逻辑运算

    python其他知识目录 1.循环打印“我是小马过河” while True:    print('我是小马过河') #4.用while从一打印到10 #5.请通过循环,1 2 3 4 5 6 8 9 ...

  10. BFC的表象认识

    首先字面翻译,这三个字母分别代表什么,box,formatting, context,它决定了元素如何对其内容进行定位,以及与其他元素的关系和相互作用. 形象点就是说一种规范,规范什么呢?规范盒子内部 ...