當前位置:成語大全網 - 新華字典 - 使用Python實現比較倆個文件的數據,不同的存在另壹個文件裏

使用Python實現比較倆個文件的數據,不同的存在另壹個文件裏

這是我之前在excel中比較兩組不同數據的代碼,修改壹下完全可以滿足妳的要求。

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

import?xlrd

import?xlwt

from?xlutils.copy?import?copy

import?os,time

import?operator

path?=?r"E:\xx"

#path?=?raw_input('Input?Path:?')

os.chdir(path)

print?"Current?Workspace:?%s"%os.getcwd()

#讀取excel工作表中數據為字典

#字典形式為:{代碼:地名}

def?readDictStandard():

#name_check?=?raw_input('Check?Excel?Name:?')

filename?=?(name_check).decode('cp936')

data?=?xlrd.open_workbook(filename?+?'.xls',formatting_info?=?True)

table?=?data.sheet_by_index(0)

#table?=?data.sheet_by_name(u'di')

print?table.name

cellList_k?=?[]

cellList_v?=?[]

ncols?=?table.ncols

for?col?in?range(0,ncols):

if?not?(col?%?2):

collist_k?=?table.col_values(col)

collist_v?=?table.col_values(col+1)

for?cell_k?in?collist_k:

cellList_k.append(cell_k)

for?cell_v?in?collist_v:

cellList_v.append(cell_v)

check?=?dict(zip(cellList_k,cellList_v))

num?=?0

for?key?in?check:

num?+=?1

#print?str(key),check[key]

print?'%d?units?in?check?Excel'%num

print?'-'*50

return?check

def?readDictCheck():

#name_check?=?raw_input('Check?Excel?Name:?')

filename?=?(name_check).decode('cp936')

data?=?xlrd.open_workbook(filename?+?'.xls',formatting_info?=?True)

table?=?data.sheet_by_index(0)

#table?=?data.sheet_by_name(u'sheet1')

print?table.name

cellList_k?=?[]

cellList_v?=?[]

ncols?=?table.ncols

collist_k?=?table.col_values(0)

collist_v?=?table.col_values(1)

for?cell_k?in?collist_k:

cellList_k.append(cell_k)

for?cell_v?in?collist_v:

cellList_v.append(cell_v)

check?=?dict(zip(cellList_k,cellList_v))

num?=?0

for?key?in?check:

num?+=?1

#print?str(key),check[key]

print?'%d?units?in?check?Excel'%num

print?'-'*50

return?check

def?checkDict(check,standard):

num?=?0

for?k?in?sorted(check.keys()):

if?k?not?in?standard.keys():

num?+=?1

print?k,check[k]

elif?check[k]?!=?standard[k]:

print?k,check[k],standard[k]

num?+=?1

print?'%d?numbers?records'%num

def?main():

global?name_check

name_check?=?raw_input('Check?Excel?Name:?')

check?=?readDictCheck()

name_check?=?raw_input('Standard?Excel?Name:?')

standard?=?readDictStandard()

time.sleep(1)

checkDict(check,standard)

if?__name__?==?"__main__":

main()

print?'-'*50