3

I am trying to create a function that combines 2 text files, and sorts them, before writing the result to a new file. I have read the existing threads about sorting files, as well as the threads about merging files, but I haven't been able to find one that answers my question.

File1:
12:24:00: 14, 15, 16
20:13:09: 1, 2, 3

File2:
08:06:02: 43, 54, 10
15:16:05: 6, 2, 12

And the desired output would be this:

NewFile:
20:13:09: 1, 2, 3
15:16:05: 6, 2, 12
12:24:00: 14, 15, 16
08:06:02: 43, 54, 10

I originally tried to merge the content of both files into one list, and then sort it, before writing it to a new file, but that didn't seem to work. Here is what I have tried so far:

def mergeandsort(file1, file2, NewFile):
    s1, s2, d=open(src1, 'r'), open(src2, 'r'), open(dst, 'w')
    l=[]
    l.append(list(s1))
    l.append(list(s2))
    n=sorted(l)
    c=''.join(str(n))
    d.write(c)
    s1.close(); s2.close(); d.close()

I am new to Python, so any help would be appreciated!

3
  • sorting is according to time? each row contain time and followed by 3 integer values? Commented Mar 23, 2016 at 4:09
  • Yes, I had to write a function that just sorted one file and using the "sorted" function worked to sort based on the first three values (which make up the time). Commented Mar 23, 2016 at 4:10
  • ok , I will give you in next 10 to 15 mints. Commented Mar 23, 2016 at 4:10

3 Answers 3

5

Trying to fix your implementation:

def mergeandsort(src1, src2, dst):
    # Use `with` statements to close file automatically
    with open(src1, 'r') as s1, open(src2, 'r') as s2, open(dst, 'w') as d:
        l = list(s1) + list(s2)
        l.sort(reverse=true)  # Since you seem to want them in reverse order...
        c = ''.join(l)
        d.write(c)

Note that this is not optimal if you manipulate big files...

Sign up to request clarification or add additional context in comments.

Comments

4

Following are step:

  1. Read files and then create list of there data.
  2. Add two list
  3. so sorted function for sorting.
  4. Use reverse method of list
  5. Write content into file. (you can do this)

Demo:

>>> p1 = '/home/vivek/Desktop/f1.txt' 
>>> p2 = '/home/vivek/Desktop/f2.txt' 
>>> 
>>> fp1 = open(p1)
>>> fp2 = open(p2)

>>> l1 = fp1.read().strip().split("\n")
>>> l1
['12:24:00: 14, 15, 16', '20:13:09: 1, 2, 3']
>>> l2 = fp2.read().strip().split("\n")
>>> l2
['08:06:02: 43, 54, 10', '15:16:05: 6, 2, 12']
>>> l3 = l1+ l2
>>> l3
['12:24:00: 14, 15, 16', '20:13:09: 1, 2, 3', '08:06:02: 43, 54, 10', '15:16:05: 6, 2, 12']
>>> sorted(l3)
['08:06:02: 43, 54, 10', '12:24:00: 14, 15, 16', '15:16:05: 6, 2, 12', '20:13:09: 1, 2, 3']
>>> merge_list = sorted(l3)
>>> merge_list.reverse()
>>> merge_list
['20:13:09: 1, 2, 3', '15:16:05: 6, 2, 12', '12:24:00: 14, 15, 16', '08:06:02: 43, 54, 10']
>>> 

Function:

def mergeandsort(file1, file2, output):
    fp1, fp2 = open(file1, 'r'), open(file2, 'r')
    merge_data = fp1.read().strip().split("\n") + fp2.read().strip().split("\n")
    merge_data = sorted(l3, reverse=True)
    fp = open(output, 'w')
    for i in merge_data:
        fp.write(i)

    fp.close()
    return True, output


p1 = '/home/vivek/Desktop/f1.txt' 
p2 = '/home/vivek/Desktop/f2.txt' 
p3 = '/home/vivek/Desktop/f12.txt' 

print mergeandsort(p1, p2, p3)

2 Comments

thank you! when I write this into another file, how do I make it so that the result goes back to being formatted into lines?
Use '\n'.join(merge_list) when writing
2

Here's an alternative that sorts based on datetime (assuming you read the contents of f1 and f2 into two lists, l1 and l2):

l1 = ['12:24:00: 14, 15, 16', '20:13:09: 1, 2, 3']
l2 = ['08:06:02: 43, 54, 10', '15:16:05: 6, 2, 12']

from datetime import datetime

for x in sorted(l1 + l2, key=lambda time_and_nums: datetime.strptime(time_and_nums.split(' ')[0][:-1], '%H:%M:%S'), reverse=True):
    print(x)

Will print

20:13:09: 1, 2, 3
15:16:05: 6, 2, 12
12:24:00: 14, 15, 16
08:06:02: 43, 54, 10

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.