Python Forum
Comapring two files - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: Comapring two files (/thread-21511.html)



Comapring two files - usman88 - Oct-02-2019

Hi, i have 2 txt files which i want to compare.

File 1 has only dates.
File 2 has dates and some text.

All i want to do is out put in File 3 is all dates of File 1 and text against those dates in file 2.

can anyone help?


RE: Comapring two files - micseydel - Oct-02-2019

What have you tried? We're happy to help you solve this yourself, but we get a lot of "do it for me"-looking posts even though we don't do people's work for them :)


RE: Comapring two files - usman88 - Oct-03-2019

(Oct-02-2019, 09:39 PM)micseydel Wrote: What have you tried? We're happy to help you solve this yourself, but we get a lot of "do it for me"-looking posts even though we don't do people's work for them :)

Hi,

You are correct, so far what i did is i could open files and then use split to extract dates only. I cant figure our how to loop that for each line in file.

I would be great if you could or anyone else could point me to topic i need to read on or some url. As i am new to python so surely have not much experience in it.


RE: Comapring two files - burningkrome - Oct-03-2019

As a suggestion, look into Python's "re" module (regex). Then you could match line by line from both files.

See here for more suggestions.

Also here for how to iterate through a file.

import re

s1 = "2019-10-03 text in the source file"
s2 = "2019-10-03 text from the file to be searched"

matches = re.search("^(\d{4}-\d{1,2}-\d{1,2})(.*)$", s1)
print(matches.group(1))

# Then use an if in
if matches.group(1) in s2:
    matches = re.search("^(\d{4}-\d{1,2}-\d{1,2})(.*)$", s2)
    print(matches.group(2))



RE: Comapring two files - usman88 - Oct-03-2019

Hi thanks for reply.

I used panda and numpy. IT works fine with small data but when i use my full file it does not work.
There are some column issues there but data is over thousand of rows so cant filter where it is messing up/


RE: Comapring two files - burningkrome - Oct-03-2019

(Oct-03-2019, 01:58 PM)usman88 Wrote: Hi thanks for reply.

I used panda and numpy. IT works fine with small data but when i use my full file it does not work.
There are some column issues there but data is over thousand of rows so cant filter where it is messing up/

Post your code so we can take a look :)


RE: Comapring two files - usman88 - Oct-07-2019

hi sorry for late reply. i got that done this python and SQL. I would go for SQL as it helps me better for future purposes and data manipulation.


RE: Comapring two files - wavic - Oct-07-2019

If the dates are formatted the same way in both files it's quite easy using dictionaries.

Make from a second file a dictionary with the dates as a key and the text as a value. Then you can get the dates from the first file and use each of it as a key to the dictionary and print the item. If there is no such a key, no match.