写入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()
本人是初学者,如果有不对的地方,希望帮忙指正,非常感谢!