Python学习(4):文本文件内容比较并写入Excel文件或文本文件(以Json为例)

写入Excel

# _*_ coding: utf-8 _*_
import os
import xlrd
str1 = []
str2 = []
str_rep = []
row_all1 = []
row_all2 = []
dir_source = "D:\\QA\\0_compare\\json - 副本\\"
dir_target = "D:\\QA\\0_compare\\zh_CN - 副本\\"
#获取dir_source下每个文件的文件名以及每个文件内容行数
for root1, dirs, files1 in os.walk(dir_source):
   #获取文件名列表
    for i in range(len(files1)):
        filename_source = files1[i]
        fa = open(root1 + filename_source, 'r', encoding='utf-8')
        #获取文件内容列表
        for line in fa.readlines():
            str1.append(line)
            #print(str1)
        count1 = 1
        ##获取文件行数列表
        for count1, line in enumerate(open(os.path.join(root1, files1[i]), "r", encoding='utf-8')):
            pass
        count1 += 1
        row_all1.append(count1)
        #print(row_all1)
#获取dir_target下每个文件的文件名以及每个文件内容行数
for root2, dirs, files2 in os.walk(dir_target):
   #获取文件名列表
    for n in range(len(files2)):
        filename_target = files2[n]
        fb = open(root2 + filename_target, 'r', encoding='utf-8')
        #获取文件内容列表
        for line in fb.readlines():
            str2.append(line)
        #print(files2)
        count2 = 1
        #获取文件行数列表
        for count2, line in enumerate(open(os.path.join(root2, files2[n]), "r", encoding='utf-8')):
            pass
        count2 += 1
        row_all2.append(count2)
        #print(count2)
        #print(row_all2)

import xlwings as xw
app = xw.App(visible=False, add_book=True)
app.display_alerts = False
app.screen_updating = False
x = 0
#列出文件名及文件行数
for a in range(len(files1)) and range(len(row_all1)):
    wb = app.books.open("D:\\QA\\0_compare\\compare result.xlsx")
    #在excel第2+x行第一列到第row_all1[a] + x + 1行第一列按内容行数输入对应文件名
    wb.sheets[0].range((2+x, 1), (row_all1[a] + x + 1, 1)).value = files1[a]
    x = x + row_all1[a]
    wb = app.books.open("D:\\QA\\0_compare\\compare result.xlsx")
    #在B2列写入所有文件内容到excel文件中
    wb.sheets[0].range('B2').options(transpose=True).value = str1
    wb.save()
    wb.close()
    #print(a)
    #print(x)
z = 0
#列出文件名及文件行数
for b in range(len(files2)) and range(len(row_all2)):
    wb = app.books.open("D:\\QA\\0_compare\\compare result.xlsx")
    wb.sheets[0].range((2 + z, 3), (row_all2[b] + z + 1, 3)).value = files2[b]
    z = z + row_all2[b]
    wb = app.books.open("D:\\QA\\0_compare\\compare result.xlsx")
    wb.sheets[0].range('D2').options(transpose=True).value = str2
    print(str2)
    wb.save()
    wb.close()

写入文本文件(此处部分代码参照其它文章,具体哪篇忘记了)

# _*_ coding: utf-8 _*_
import os
str1 = []
str2 = []
str_rep = []
dir_source = "D:\\QA\\0_compare\\json\\"
dir_target = "D:\\QA\\0_compare\\zh_CN\\"
fc = open("D:\\QA\\0_compare\\compare.txt", 'w+', encoding='UTF-8')
for root1, dirs, files1 in os.walk(dir_source):
    for i in range(len(files1)):
        filename_source = files1[i]
        fa = open(root1 + filename_source, 'r', encoding='utf-8')
        print(filename_source)
        for line in fa.readlines():
            str1.append(line)
            print(str1)
for root2, dirs, files2 in os.walk(dir_target):
    for n in range(len(files2)):
        filename_target = files2[n]
        fb = open(root2 + filename_target, 'r', encoding='utf-8')
        print(filename_target)
        for line in fb.readlines():
            str2.append(line)
            print(line)
            #print(str2)
# 将两个文件中重复的行,添加到str_dump中

for m in str1:
    if m in str2:
        str_rep.append(m)
# 将两个文件的行合并,并去重
str_all = set(str1 + str2)

for i in str_rep:
    if i in str_all:
        print(str_all.remove(i))
# 写入文件中
for x in list(str_all):
    fc.writelines(os.path.join(root2, filename_target) + "  " + str(x) + '\n')

fa.close()
fb.close()
fc.close()

本人是初学者,如果有不对的地方,希望帮忙指正,非常感谢!

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值