compare two different files text by text using pythonHow to merge two dictionaries in a single expression?How do I check whether a file exists without exceptions?Calling an external command in PythonWhat are metaclasses in Python?What is the difference between @staticmethod and @classmethod?What is the difference between Python's list methods append and extend?How can I safely create a nested directory in Python?How do I concatenate two lists in Python?How do I list all files of a directory?Does Python have a string 'contains' substring method?

Why did they go to Dragonstone?

What was the plan for an abort of the Enola Gay's mission to drop the atomic bomb?

What was the notion of limit that Newton used?

Why did Captain America age?

How to select certain lines (n, n+4, n+8, n+12...) from the file?

Why does increasing the sampling rate make implementing an anti-aliasing filter easier?

Ex-manager wants to stay in touch, I don't want to

Examples where existence is harder than evaluation

Was there a contingency plan in place if Little Boy failed to detonate?

How did Thanos not realise this had happened at the end of Endgame?

When do you stop "pushing" a book?

How to slow yourself down (for playing nice with others)

What can cause an unfrozen indoor copper drain pipe to crack?

How can I avoid subordinates and coworkers leaving work until the last minute, then having no time for revisions?

Peculiarities in low dimensions or low order or etc

Does the 500 feet falling cap apply per fall, or per turn?

Why is the Sun made of light elements only?

Is every story set in the future "science fiction"?

Can the president of the United States be guilty of insider trading?

What do "KAL." and "A.S." stand for in this inscription?

Has magnetic core memory been used beyond the Moon?

How do I compare the result of "1d20+x, with advantage" to "1d20+y, without advantage", assuming x < y?

Is there a need for better software for writers?

My perfect evil overlord plan... or is it?



compare two different files text by text using python


How to merge two dictionaries in a single expression?How do I check whether a file exists without exceptions?Calling an external command in PythonWhat are metaclasses in Python?What is the difference between @staticmethod and @classmethod?What is the difference between Python's list methods append and extend?How can I safely create a nested directory in Python?How do I concatenate two lists in Python?How do I list all files of a directory?Does Python have a string 'contains' substring method?






.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty height:90px;width:728px;box-sizing:border-box;








1















i am trying to find same words/text between two different file but didn't get the result which i'm looking for.



i have tried to compare line by line but didn't get the result



with open('top_1k_domain.txt', 'r') as file1:
with open('latesteasylist.txt', 'r') as file2:
same = set(file1).intersection(file2)

same.discard('n')

with open('some_output_file1.txt', 'w') as file_out:
for line in same:
file_out.write(line)


like my first file containing the text



 google.com
youtube.com
facebook.com
doublepimp.com
uod2quk646.com
qq.com
yahoo.com
tmall.com


where as the second file contains



 ||doublepimp.com^$third-party
||uod2quk646.com^$third-party
....etc


it did not produce output which i m looking for that there should be
doublepimp.com
and uod2quk646.com in the some_output_file1.txt file but its empty.can any body help me out here










share|improve this question
























  • Hello, I hope you are doing well, Could you give us and example of the two files you use? and the wished output? Please. Thank you in advance.

    – Guillaume Lastecoueres
    Mar 23 at 9:53












  • first file contain the domain name where as second file contain the filter rule . i have to check that for which domain name the rule is described in the filter rule. i m trying to extract the domain name from both file which are common and for which rule is defined so your response will be apriciated @GuillaumeLastecoueres thanks

    – kashifbilal kashi
    Mar 23 at 11:07


















1















i am trying to find same words/text between two different file but didn't get the result which i'm looking for.



i have tried to compare line by line but didn't get the result



with open('top_1k_domain.txt', 'r') as file1:
with open('latesteasylist.txt', 'r') as file2:
same = set(file1).intersection(file2)

same.discard('n')

with open('some_output_file1.txt', 'w') as file_out:
for line in same:
file_out.write(line)


like my first file containing the text



 google.com
youtube.com
facebook.com
doublepimp.com
uod2quk646.com
qq.com
yahoo.com
tmall.com


where as the second file contains



 ||doublepimp.com^$third-party
||uod2quk646.com^$third-party
....etc


it did not produce output which i m looking for that there should be
doublepimp.com
and uod2quk646.com in the some_output_file1.txt file but its empty.can any body help me out here










share|improve this question
























  • Hello, I hope you are doing well, Could you give us and example of the two files you use? and the wished output? Please. Thank you in advance.

    – Guillaume Lastecoueres
    Mar 23 at 9:53












  • first file contain the domain name where as second file contain the filter rule . i have to check that for which domain name the rule is described in the filter rule. i m trying to extract the domain name from both file which are common and for which rule is defined so your response will be apriciated @GuillaumeLastecoueres thanks

    – kashifbilal kashi
    Mar 23 at 11:07














1












1








1








i am trying to find same words/text between two different file but didn't get the result which i'm looking for.



i have tried to compare line by line but didn't get the result



with open('top_1k_domain.txt', 'r') as file1:
with open('latesteasylist.txt', 'r') as file2:
same = set(file1).intersection(file2)

same.discard('n')

with open('some_output_file1.txt', 'w') as file_out:
for line in same:
file_out.write(line)


like my first file containing the text



 google.com
youtube.com
facebook.com
doublepimp.com
uod2quk646.com
qq.com
yahoo.com
tmall.com


where as the second file contains



 ||doublepimp.com^$third-party
||uod2quk646.com^$third-party
....etc


it did not produce output which i m looking for that there should be
doublepimp.com
and uod2quk646.com in the some_output_file1.txt file but its empty.can any body help me out here










share|improve this question
















i am trying to find same words/text between two different file but didn't get the result which i'm looking for.



i have tried to compare line by line but didn't get the result



with open('top_1k_domain.txt', 'r') as file1:
with open('latesteasylist.txt', 'r') as file2:
same = set(file1).intersection(file2)

same.discard('n')

with open('some_output_file1.txt', 'w') as file_out:
for line in same:
file_out.write(line)


like my first file containing the text



 google.com
youtube.com
facebook.com
doublepimp.com
uod2quk646.com
qq.com
yahoo.com
tmall.com


where as the second file contains



 ||doublepimp.com^$third-party
||uod2quk646.com^$third-party
....etc


it did not produce output which i m looking for that there should be
doublepimp.com
and uod2quk646.com in the some_output_file1.txt file but its empty.can any body help me out here







python python-3.x






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Mar 23 at 10:09









blhsing

46.2k51747




46.2k51747










asked Mar 23 at 9:47









kashifbilal kashikashifbilal kashi

127




127












  • Hello, I hope you are doing well, Could you give us and example of the two files you use? and the wished output? Please. Thank you in advance.

    – Guillaume Lastecoueres
    Mar 23 at 9:53












  • first file contain the domain name where as second file contain the filter rule . i have to check that for which domain name the rule is described in the filter rule. i m trying to extract the domain name from both file which are common and for which rule is defined so your response will be apriciated @GuillaumeLastecoueres thanks

    – kashifbilal kashi
    Mar 23 at 11:07


















  • Hello, I hope you are doing well, Could you give us and example of the two files you use? and the wished output? Please. Thank you in advance.

    – Guillaume Lastecoueres
    Mar 23 at 9:53












  • first file contain the domain name where as second file contain the filter rule . i have to check that for which domain name the rule is described in the filter rule. i m trying to extract the domain name from both file which are common and for which rule is defined so your response will be apriciated @GuillaumeLastecoueres thanks

    – kashifbilal kashi
    Mar 23 at 11:07

















Hello, I hope you are doing well, Could you give us and example of the two files you use? and the wished output? Please. Thank you in advance.

– Guillaume Lastecoueres
Mar 23 at 9:53






Hello, I hope you are doing well, Could you give us and example of the two files you use? and the wished output? Please. Thank you in advance.

– Guillaume Lastecoueres
Mar 23 at 9:53














first file contain the domain name where as second file contain the filter rule . i have to check that for which domain name the rule is described in the filter rule. i m trying to extract the domain name from both file which are common and for which rule is defined so your response will be apriciated @GuillaumeLastecoueres thanks

– kashifbilal kashi
Mar 23 at 11:07






first file contain the domain name where as second file contain the filter rule . i have to check that for which domain name the rule is described in the filter rule. i m trying to extract the domain name from both file which are common and for which rule is defined so your response will be apriciated @GuillaumeLastecoueres thanks

– kashifbilal kashi
Mar 23 at 11:07













2 Answers
2






active

oldest

votes


















1














By using set intersection, the items in the two sets will only match if they are identical, which they are not in the case of the two files, since the lines in the second file contain not just the domain names, but also other AdBlock syntax.



You should extract the domain name portion from the lines in the second file before you perform a set intersection with lines in the first file:



import re
same = set(file1).intersection((re.findall(r'[a-z0-9.-]+', line) or [''])[0] + 'n' for line in file2)





share|improve this answer

























  • it getting an AttributeError: 'NoneType' object has no attribute 'group' what i'm missing here

    – kashifbilal kashi
    Mar 23 at 10:45











  • That's because some of the lines in your second file do not have a domain name at all. I've updated my answer so that those lines are ignored.

    – blhsing
    Mar 23 at 10:53











  • i have an other question if you will do it for me i'll be thankfull to you @blhsing i am also trying to fetch the type of rule which contain only this category of rule /example.js $script,domain=example.com will you make me patteren for this so that i can fetch this type of rule from the filter list ?

    – kashifbilal kashi
    Mar 23 at 11:18












  • Glad to be of help. That really is out of the scope of this question though. Please ask about this in a new question with formatted code so that people can better help.

    – blhsing
    Mar 23 at 11:25






  • 1





    sure thanks again

    – kashifbilal kashi
    Mar 23 at 11:32


















0














The core idea is okay, but since the second file contains more than just the domain, you will need to strip that out first.



||example.com^$third-party will never equal example.com



One possibility:



same = set(file1).itersection(set(x[2, x.index('^')-2]+'n' for x in file2))





share|improve this answer























  • its getting an error that substring not found , Could you please complete my code @mhhollomon because still i im in the learning stage

    – kashifbilal kashi
    Mar 23 at 10:58











Your Answer






StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");

StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);

else
createEditor();

);

function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);



);













draft saved

draft discarded


















StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55312479%2fcompare-two-different-files-text-by-text-using-python%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown

























2 Answers
2






active

oldest

votes








2 Answers
2






active

oldest

votes









active

oldest

votes






active

oldest

votes









1














By using set intersection, the items in the two sets will only match if they are identical, which they are not in the case of the two files, since the lines in the second file contain not just the domain names, but also other AdBlock syntax.



You should extract the domain name portion from the lines in the second file before you perform a set intersection with lines in the first file:



import re
same = set(file1).intersection((re.findall(r'[a-z0-9.-]+', line) or [''])[0] + 'n' for line in file2)





share|improve this answer

























  • it getting an AttributeError: 'NoneType' object has no attribute 'group' what i'm missing here

    – kashifbilal kashi
    Mar 23 at 10:45











  • That's because some of the lines in your second file do not have a domain name at all. I've updated my answer so that those lines are ignored.

    – blhsing
    Mar 23 at 10:53











  • i have an other question if you will do it for me i'll be thankfull to you @blhsing i am also trying to fetch the type of rule which contain only this category of rule /example.js $script,domain=example.com will you make me patteren for this so that i can fetch this type of rule from the filter list ?

    – kashifbilal kashi
    Mar 23 at 11:18












  • Glad to be of help. That really is out of the scope of this question though. Please ask about this in a new question with formatted code so that people can better help.

    – blhsing
    Mar 23 at 11:25






  • 1





    sure thanks again

    – kashifbilal kashi
    Mar 23 at 11:32















1














By using set intersection, the items in the two sets will only match if they are identical, which they are not in the case of the two files, since the lines in the second file contain not just the domain names, but also other AdBlock syntax.



You should extract the domain name portion from the lines in the second file before you perform a set intersection with lines in the first file:



import re
same = set(file1).intersection((re.findall(r'[a-z0-9.-]+', line) or [''])[0] + 'n' for line in file2)





share|improve this answer

























  • it getting an AttributeError: 'NoneType' object has no attribute 'group' what i'm missing here

    – kashifbilal kashi
    Mar 23 at 10:45











  • That's because some of the lines in your second file do not have a domain name at all. I've updated my answer so that those lines are ignored.

    – blhsing
    Mar 23 at 10:53











  • i have an other question if you will do it for me i'll be thankfull to you @blhsing i am also trying to fetch the type of rule which contain only this category of rule /example.js $script,domain=example.com will you make me patteren for this so that i can fetch this type of rule from the filter list ?

    – kashifbilal kashi
    Mar 23 at 11:18












  • Glad to be of help. That really is out of the scope of this question though. Please ask about this in a new question with formatted code so that people can better help.

    – blhsing
    Mar 23 at 11:25






  • 1





    sure thanks again

    – kashifbilal kashi
    Mar 23 at 11:32













1












1








1







By using set intersection, the items in the two sets will only match if they are identical, which they are not in the case of the two files, since the lines in the second file contain not just the domain names, but also other AdBlock syntax.



You should extract the domain name portion from the lines in the second file before you perform a set intersection with lines in the first file:



import re
same = set(file1).intersection((re.findall(r'[a-z0-9.-]+', line) or [''])[0] + 'n' for line in file2)





share|improve this answer















By using set intersection, the items in the two sets will only match if they are identical, which they are not in the case of the two files, since the lines in the second file contain not just the domain names, but also other AdBlock syntax.



You should extract the domain name portion from the lines in the second file before you perform a set intersection with lines in the first file:



import re
same = set(file1).intersection((re.findall(r'[a-z0-9.-]+', line) or [''])[0] + 'n' for line in file2)






share|improve this answer














share|improve this answer



share|improve this answer








edited Mar 23 at 10:52

























answered Mar 23 at 10:07









blhsingblhsing

46.2k51747




46.2k51747












  • it getting an AttributeError: 'NoneType' object has no attribute 'group' what i'm missing here

    – kashifbilal kashi
    Mar 23 at 10:45











  • That's because some of the lines in your second file do not have a domain name at all. I've updated my answer so that those lines are ignored.

    – blhsing
    Mar 23 at 10:53











  • i have an other question if you will do it for me i'll be thankfull to you @blhsing i am also trying to fetch the type of rule which contain only this category of rule /example.js $script,domain=example.com will you make me patteren for this so that i can fetch this type of rule from the filter list ?

    – kashifbilal kashi
    Mar 23 at 11:18












  • Glad to be of help. That really is out of the scope of this question though. Please ask about this in a new question with formatted code so that people can better help.

    – blhsing
    Mar 23 at 11:25






  • 1





    sure thanks again

    – kashifbilal kashi
    Mar 23 at 11:32

















  • it getting an AttributeError: 'NoneType' object has no attribute 'group' what i'm missing here

    – kashifbilal kashi
    Mar 23 at 10:45











  • That's because some of the lines in your second file do not have a domain name at all. I've updated my answer so that those lines are ignored.

    – blhsing
    Mar 23 at 10:53











  • i have an other question if you will do it for me i'll be thankfull to you @blhsing i am also trying to fetch the type of rule which contain only this category of rule /example.js $script,domain=example.com will you make me patteren for this so that i can fetch this type of rule from the filter list ?

    – kashifbilal kashi
    Mar 23 at 11:18












  • Glad to be of help. That really is out of the scope of this question though. Please ask about this in a new question with formatted code so that people can better help.

    – blhsing
    Mar 23 at 11:25






  • 1





    sure thanks again

    – kashifbilal kashi
    Mar 23 at 11:32
















it getting an AttributeError: 'NoneType' object has no attribute 'group' what i'm missing here

– kashifbilal kashi
Mar 23 at 10:45





it getting an AttributeError: 'NoneType' object has no attribute 'group' what i'm missing here

– kashifbilal kashi
Mar 23 at 10:45













That's because some of the lines in your second file do not have a domain name at all. I've updated my answer so that those lines are ignored.

– blhsing
Mar 23 at 10:53





That's because some of the lines in your second file do not have a domain name at all. I've updated my answer so that those lines are ignored.

– blhsing
Mar 23 at 10:53













i have an other question if you will do it for me i'll be thankfull to you @blhsing i am also trying to fetch the type of rule which contain only this category of rule /example.js $script,domain=example.com will you make me patteren for this so that i can fetch this type of rule from the filter list ?

– kashifbilal kashi
Mar 23 at 11:18






i have an other question if you will do it for me i'll be thankfull to you @blhsing i am also trying to fetch the type of rule which contain only this category of rule /example.js $script,domain=example.com will you make me patteren for this so that i can fetch this type of rule from the filter list ?

– kashifbilal kashi
Mar 23 at 11:18














Glad to be of help. That really is out of the scope of this question though. Please ask about this in a new question with formatted code so that people can better help.

– blhsing
Mar 23 at 11:25





Glad to be of help. That really is out of the scope of this question though. Please ask about this in a new question with formatted code so that people can better help.

– blhsing
Mar 23 at 11:25




1




1





sure thanks again

– kashifbilal kashi
Mar 23 at 11:32





sure thanks again

– kashifbilal kashi
Mar 23 at 11:32













0














The core idea is okay, but since the second file contains more than just the domain, you will need to strip that out first.



||example.com^$third-party will never equal example.com



One possibility:



same = set(file1).itersection(set(x[2, x.index('^')-2]+'n' for x in file2))





share|improve this answer























  • its getting an error that substring not found , Could you please complete my code @mhhollomon because still i im in the learning stage

    – kashifbilal kashi
    Mar 23 at 10:58















0














The core idea is okay, but since the second file contains more than just the domain, you will need to strip that out first.



||example.com^$third-party will never equal example.com



One possibility:



same = set(file1).itersection(set(x[2, x.index('^')-2]+'n' for x in file2))





share|improve this answer























  • its getting an error that substring not found , Could you please complete my code @mhhollomon because still i im in the learning stage

    – kashifbilal kashi
    Mar 23 at 10:58













0












0








0







The core idea is okay, but since the second file contains more than just the domain, you will need to strip that out first.



||example.com^$third-party will never equal example.com



One possibility:



same = set(file1).itersection(set(x[2, x.index('^')-2]+'n' for x in file2))





share|improve this answer













The core idea is okay, but since the second file contains more than just the domain, you will need to strip that out first.



||example.com^$third-party will never equal example.com



One possibility:



same = set(file1).itersection(set(x[2, x.index('^')-2]+'n' for x in file2))






share|improve this answer












share|improve this answer



share|improve this answer










answered Mar 23 at 10:16









mhhollomonmhhollomon

539314




539314












  • its getting an error that substring not found , Could you please complete my code @mhhollomon because still i im in the learning stage

    – kashifbilal kashi
    Mar 23 at 10:58

















  • its getting an error that substring not found , Could you please complete my code @mhhollomon because still i im in the learning stage

    – kashifbilal kashi
    Mar 23 at 10:58
















its getting an error that substring not found , Could you please complete my code @mhhollomon because still i im in the learning stage

– kashifbilal kashi
Mar 23 at 10:58





its getting an error that substring not found , Could you please complete my code @mhhollomon because still i im in the learning stage

– kashifbilal kashi
Mar 23 at 10:58

















draft saved

draft discarded
















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid


  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55312479%2fcompare-two-different-files-text-by-text-using-python%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Kamusi Yaliyomo Aina za kamusi | Muundo wa kamusi | Faida za kamusi | Dhima ya picha katika kamusi | Marejeo | Tazama pia | Viungo vya nje | UrambazajiKuhusu kamusiGo-SwahiliWiki-KamusiKamusi ya Kiswahili na Kiingerezakuihariri na kuongeza habari

Swift 4 - func physicsWorld not invoked on collision? The Next CEO of Stack OverflowHow to call Objective-C code from Swift#ifdef replacement in the Swift language@selector() in Swift?#pragma mark in Swift?Swift for loop: for index, element in array?dispatch_after - GCD in Swift?Swift Beta performance: sorting arraysSplit a String into an array in Swift?The use of Swift 3 @objc inference in Swift 4 mode is deprecated?How to optimize UITableViewCell, because my UITableView lags

Access current req object everywhere in Node.js ExpressWhy are global variables considered bad practice? (node.js)Using req & res across functionsHow do I get the path to the current script with Node.js?What is Node.js' Connect, Express and “middleware”?Node.js w/ express error handling in callbackHow to access the GET parameters after “?” in Express?Modify Node.js req object parametersAccess “app” variable inside of ExpressJS/ConnectJS middleware?Node.js Express app - request objectAngular Http Module considered middleware?Session variables in ExpressJSAdd properties to the req object in expressjs with Typescript