Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Comapring two files
#1
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?
Reply
#2
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 :)
Reply
#3
(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.
Reply
#4
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))
Reply
#5
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/
Reply
#6
(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 :)
Reply
#7
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.
Reply
#8
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.
"As they say in Mexico 'dosvidaniya'. That makes two vidaniyas."
https://freedns.afraid.org
Reply


Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020