0

I have a string of numbers with no whitespaces like this:

s = '12.2321.4310.85'

I know that the format for each number is F5.2 (I am reading the string from a FORTRAN code output)

I need to obtain the following list of numbers based on s:

[12.23,21.43,10.85]

How can I do this in python?

Thanks in advance for any help!

9
  • 4
    There is something very very wrong with whatever is giving this data to you. Fix that instead of writing hacks to deal with it. Commented Dec 13, 2019 at 19:26
  • @cs95 This is from a text file output from a model written in FORTRAN. I am reading the text file because I need to modify this string of numbers later. Commented Dec 13, 2019 at 19:32
  • Slice the string into chunks of 5 characters. Convert each chunk to float. Commented Dec 13, 2019 at 19:32
  • Yup, that's my idea too! @Prune Commented Dec 13, 2019 at 19:32
  • BTW, what you've listed is FORTRAN format specification F5.2, not 4.2. It's total field width at the front. Commented Dec 13, 2019 at 19:35

3 Answers 3

3

Slice the string into chunks of 5 characters. Convert each chunk to float.

>>> [float(s[i:i+5]) for i in range(0, len(s), 5)]
[12.23, 21.43, 10.85]
Sign up to request clarification or add additional context in comments.

3 Comments

There might be numbers like 1234.56 and 78.99 in the dataset using format 4.2 and generate s = "1234.5678.99"
Thank you @Prune, this works for me since I know the number of characters from the FORTRAN format.
@aminrd I just clarified with OP that this is not the case. Strict FORTRAN does not overrun the output field as other languages do ... often to my annoyance in college days.
0

If you are really sure of the format, and that will always be handed in that way then using a step of 5 in your loop might work:

s = '12.2321.4310.85'
output = []
for i in range(0,len(s),5):
    output.append(float(s[i:i+5]))
print(output)

Output:

[12.23, 21.43, 10.85]

4 Comments

I'm not sure, but couldn't format 4.2 generate something like 1234.56 in python?
Yes, the size of 5 assumes there's 2 decimal places, 1 ones and 1 tens.
My point is, there might be numbers like 1234.56 and 78.99 in the dataset using format 4.2 and generate s = "1234.5678.99"
Yes, that's why I specified the condition in my previous comment.
0

I think the safest way is to rely on . points. Because we know that every floating point should have one fraction and always there are two fraction numbers (there might be values like 1234.56 and 78.99 in the data that generates s = "1234.5678.99"). But we are not sure how many digits are before .. So we can extract values one by one based on ..

s = '12.2321.4310.85'
def extractFloat(s):
    # Extracts the first floating number with 2 floatings from the string
    return float( s[:s.find('.')+3]) , s[s.find('.')+3:]

l = []
while len(s) > 0:
    value, s = extractFloat(s)
    l.append(value)

print(l)
# Output: 
# [12.23, 21.43, 10.85]

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.