J. P. -4 years ago 118
Python Question

Python: Have lines of mixed characters and floats: how to add up the floats?

I have a file (say, "myfile") that looks something like this:

``````4
1.0 0 0
0.0 1.0 0.0
0.0 0.0 1.0
C  0.0 0.0 -0.5
C  0.0 0.0 0.82
H  0.0 0.9 -1.0
H  0.0 -0.9 -1.0
``````

The first number (in this case, 4) determines how many lines there are with a character (such as C or H) at the beginning.

What I'm trying to do is read the numbers after the character and do some math with them. In this case, I want to calculate distances between lines 5 and 6, lines 5 and 7, and lines 5 and 8 (and then distances between lines 6 and 7 and 6 and 8, and then distances between lines 7 and 8). So something like this:

5 and 6: (0.0 - 0.0)^2 + (0.0 - 0.0)^2 + (-0.5 - 0.82)^2

5 and 7: (0.0 - 0.0)^2 + (0.0 - 0.9)^2 + (-0.5 - -1.0)^2

and so on.

To do this means that I need to read in the line, and then convert only the numbers to floats, ignoring the character. Would anyone have any suggestions for how to do this? Very tentatively I have:

``````with open("myfile.txt", "r") as inputs:
print lines[4: sum(1 for _ in inputs)]
``````

This gives me access to these 4 lines each as strings:

``````['C     0.0   0.0  -0.5\n', 'C     0.0    0.0    0.8\n', 'H     0.000000    0.9   -1.0\n', 'H     0.000000   -0.9   -1.0\n']
``````

However, I need to figure out how to get the numbers into floats to then compare them.

Any pointers would be very much appreciated/upvoted. Thank you!

Answer Source

Something like this maybe:

``````lines = ['C     0.0   0.0  -0.5\n', 'C     0.0    0.0    0.8\n', 'H     0.000000    0.9   -1.0\n', 'H     0.000000   -0.9   -1.0\n']

new_lines = [[float(x) for x in line.split()[1:]] for line in lines]

print(new_lines)
``````

results in:

``````[[0.0, 0.0, -0.5], [0.0, 0.0, 0.8], [0.0, 0.9, -1.0], [0.0, -0.9, -1.0]]
``````

basically splitting elements of the line (without argument so it trims all extra whitespace and linefeed to avoid empty strings) and dropping the first one since it doesn't represent a `float`.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download
Latest added