0

I have data like this in a .dat format

1 13 0.54 

1 15 0.65

1 67 0.55

2 355 0.54

2 456 0.29

3 432 0.55

3 542 0.333

I want to merge the rows starting with 1, 2 and so on and want a final file like this:

1 13 0.54 15 0.65 67 0.55

2 355 0.54 456 0.29

3 432 0.55 542 0.333

Can someone please help me? I am new to Python. Unless I get this format file I cannot run my abaqus code.

2
  • 2
    hint: set the first col as dict keys. And also read setdefault or defaultdict docs. Commented Aug 9, 2015 at 5:33
  • Does my answer help? Commented Aug 10, 2015 at 6:26

2 Answers 2

1

Explanation - first we split the file into lines, and then we split the lines on white space.

We then use itertools.groupby to group the lines by their first element.

We then take the values, ignore the first element and join on spaces, and prepend the key that we were grouping by and a space.

from itertools import groupby
with open("file.dat") as f:
    lines = [line.split() for line in f.readlines()]
    filteredlines = [line for line in lines if len(line)]
    for k, v in groupby(filteredlines, lambda x: x[0]):
        print k + " " + " ".join([" ".join(velem[1:]) for velem in v])
        print
Sign up to request clarification or add additional context in comments.

Comments

1

The Python CSV library can be used to both read your DAT file and also create your CSV file as follows:

import csv, itertools

with open("input.dat", "r") as f_input, open("output.csv", "wb") as f_output:
    csv_input = csv.reader(f_input, delimiter=" ", skipinitialspace=True)
    csv_output = csv.writer(f_output, delimiter=" ")
    dat = [line for line in csv_input if len(line)]

    for k, g in itertools.groupby(dat, lambda x: x[0]):
        csv_output.writerow([k] + list(itertools.chain.from_iterable([value[1:] for value in g])))

It produces an output file as follows:

1 13 0.54 15 0.65 67 0.55
2 355 0.54 456 0.29
3 432 0.55 542 0.333

Tested using Python 2.7

3 Comments

Sorry for the late reply.I work only on weekends.So I haven't seen it.
I am getting this error saying there is no built in function 'len'.Could you please hrlp me
Try now. There was a typo. It should have been line not len.

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.