我正在开发一个python代码,该代码将比较txt文件和csv文件并找出是否相同。如果不相同,请找出错误并在excel表上进行总结。
def main():
filename1=input("Enter txt file name:- ")
filename2=input("Enter csv file name:- ")
fp1=open(filename1,"r")
fp2=open(filename2,"r")
list1=[]
list2=[]
for line in fp1: #iterating through each line in the file
a=line.split(",") #splitting line based on comma
for i in a: #iterating through each element in list
i=i.rstrip() #removing new line from elements in list
list1.append(i) #appending element in the list
for line in fp2:
a=line.split(",")
for i in a:
i=i.rstrip()
list2.append(i)
fp2=open("res.csv","a") #opening res file in append mode
flag=0
for i in range(0,len(list1)): #iterating through lists
if (i==len(list1)-1 and list1[i]!=list2[i]): #if total is different in both files
fp2.write(list1[i-1]+","+str(abs(int(list1[i])-int(list2[i])))) #printing difference
flag=1
elif (list1[i]!=list2[i]): #if other lines are different
fp2.write(list1[i-1]+","+list1[i]+","+list2[i-1]+","+list2[i]+"\n") #printing different lines
flag=1
if (flag==0): #if there is no difference
fp2.write("none")
main() #calling main function
输出应该是一个excel表,其中总结了两个文件之间的差异。上面的代码给出了文件之间的数字差异,但是如果行数不同,则输出还应该打印以下行不在1个文件中。我希望能有任何想法来改进此代码,并帮助创建代码以将txt和csv文件与excel电子表格上的差异输出进行比较。 谢谢。 *我在这里还很陌生,所以如果需要编辑某些内容或使问题的一部分更清楚,请告诉我。
答案 0 :(得分:0)
如果两个文件都包含相同类型的数据,则建议将.csv和.txt数据都读取到pandas数据表中(转换后)。
阅读后,您可以轻松地处理两个数据集的列和行,即找到两个表之间的差异,并将此差异输出为所需的任何格式。