0
Numbers parsing in a big string
If I have a huge data of numbers but all stored in a large string. How can I covert it back easily to numbers? Data="12.3E+001, 11.5E+002, 78.1E+006, .... " I have a long undersized way to solve this. But I want an officiant way to solve it.
3 Réponses
+ 1
You can just parse a number string in scientific notation to float. To do it for multiple numbers in a single string, split it first. You can output scientific notation with format specifier "e"
Data = "12.3E+001, 11.5E+002, 78.1E+006"
numbers = list(map(float, Data.split(", ")))
print(*numbers)
print(*map("{:e}".format, numbers))
0
Benjamin Jürgens thank you. This works. Can you please point me to an article that I can read about the map and float? I haven't done list, map, float all in one line before. So any article will be helpful.
0
I'm using function chaining there: the return value of one function serves as argument for the next. You could rewrite it like this:
number_strings = Data.split(", ")
mapped_numbers = map(float, number_strings)
numbers = list(mapped_numbers)
map is explained in Python core/Functional programming (66.1) and functions as arguments in Python core/Functions & modules (37.1)
float is a converter function and returns its argument as float