1

I am very new to Python and programming. I have a file with measurment values that looks like this:

85.2147....85.2150..85.2152..85.2166...85.2180.85.2190.

At the end what I want to have is:
85.2147
85.2150
85.2152
85.2166
85.2180
85.2190

What I managed using

k = (j.replace('....', '\r'))
l = (k.replace('...', '\r'))
m = (l.replace('..', '\r'))

is this:

85.2147
85.2150
85.2152
85.2166
85.2180.85.2190.

Now, the question is, how can I conditionally replace single dots, A) if no numbers come after it; and B) if the number after the point is the same as the 6th number (or 7th character) before the point.

1
  • Use regex module Commented Sep 11, 2020 at 9:42

2 Answers 2

1
>>> import re
>>> string = '85.2147....85.2150..85.2152..85.2166...85.2180.85.2190.'
>>> result = re.findall('(\d+\.\d+)\.+',string)
>>> result
['85.2147', '85.2150', '85.2152', '85.2166', '85.2180', '85.2190']
Sign up to request clarification or add additional context in comments.

Comments

1

You should read up on Python regular expressions and book mark a regular expression test page to help you solve these problems. Also, it's good to remember that Python can be run in interactive mode to allow you to test things out by hand very quickly.

>>> import re
>>> test = "85.2147....85.2150..85.2152..85.2166...85.2180.85.2190."
>>> target = re.compile(r'(\d+\.\d+)(\.+)')
>>> match = re.findall(target, test)
>>> match
[('85.2147', '....'), ('85.2150', '..'), ('85.2152', '..'), ('85.2166', '...'), ('85.2180', '.'), ('85.2190', '.')]
>>> res
['85.2147', '85.2150', '85.2152', '85.2166', '85.2180', '85.2190']
>>>

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.