简体   繁体   English

将txt文件与csv文件与python进行比较

[英]comparing a txt file with csv file with python

I am working on developing a python code that would compare a txt file and a csv file and find out if there are identical or not.If not,find the errors and summarize them on a excel table. 我正在开发一个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

The output should be an excel table with a summary of the differences between the 2 files.The above code gives the difference in numbers between the files but if the number of lines is different, the output should also print the lines that are not in 1 file. 输出应该是一个excel表,其中包含两个文件之间差异的摘要。上面的代码给出了文件之间的数字差异,但如果行数不同,输出也应该打印不在1的行文件。 I would appreciate any ideas to improve this code and help creating the code to compare a txt and csv file with an output of the difference on a excel spreadsheet. 我将不胜感激任何改进此代码的想法,并帮助创建代码以比较txt和csv文件与excel电子表格上的差异输出。 Thank you. 谢谢。 * I am still new here so please let me know if I need to edit something or make a part of my question more clear. *我还是新来的,所以如果我需要编辑某些内容或让我的问题更清楚,请告诉我。

If both files contains the same type of data, I'd recommend to read both .csv and .txt data to pandas data table (after transformation). 如果两个文件包含相同类型的数据,我建议将.csv和.txt数据同时读取到pandas数据表(转换后)。

After the reading you can easily operate with columns and rows of both datasets, ie find the difference between the two tables, and output this difference to any format you want. 读取后,您可以轻松地使用两个数据集的列和行进行操作,即找到两个表之间的差异,并将此差异输出到您想要的任何格式。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM