程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式師世界 >> 編程語言 >  >> 更多編程語言 >> Python

Python quick comparison of string differences

編輯:Python

In daily life , Often for some characters , List, etc , Find out the difference . For example, it can be in excel Application mid and exact Combine to compare each character , If you encounter a long string , Obviously, it is only through the formula to compare , The workload is very heavy , Even if passed VBA Join the loop , Reduce the amount of repeated formulas , The results are also scattered , Difficult to judge quickly .
str_1 : payload%3D%5B%7B%22code%22%3A%22city%22%2C%22name%22%3A%22%E4%B8%9C%E8%8E%9E%22%2C%22value%22%3A%222665%22%2C%22jsver%3Daplus_std%26lver%3D8.15.6%26pver%3D0.7.11%26cache%3Dd65781a%26page_cna%3DfE0tGdHBzCoCAXFtdrUNRwMz%26_slog%3D0
str_2: payload%3D%5B%7B%22code%22%3A%22city%22%2C%22name%22%3A%22%E4%B8%AD%E5%B1%B1%22%2C%22value%22%3A%222667%22%2C%22jsver%3Daplus_std%26lver%3D8.15.6%26pver%3D0.7.11%26cache%3Dbf6e196%26page_cna%3DfE0tGdHBzCoCAXFtdrUNRwMz%26_slog%3D0
Compare and analyze the above two requests URL suffix For example , It can be seen that the parts of the two requests are very similar . First, compare the whole string , Here's the picture 1
The results show that The two strings do differ , Which part is it ? You can import difflib library , Using it compare() Methods can quickly identify specific differences .
First, through observation, it can be found that , In the request URL The suffix part of , It all includes payload, name,value Other key words , You can split it into separate lines first , Print diff Comment on its type , It's a generator object , Here's the picture 2
Try to put diff Convert to a list and print , It is found that the output results are confused , It's hard to see the result of comparison , Here's the picture 3
Convert the list to a string of lines ,'\n'.join(list(diff)), Finally, you can output very clear comparison results , Here's the picture 4
The definition of specific compliance , Here's the picture 5,difflib library There are other applications , For details, please refer to its official documents .

chart 1


chart 2


chart 3


chart 4


chart 5


  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式師世界 All Rights Reserved