0

I'm trying to figure out how to read a certain part of a string using python, but I can't seem to figure it out, and nobody has the solution I'm looking for.

I have multiple lines formatted similarly to this:

1235:9875:0.1234

Its separated with colons, but the thing is that the length of the line varies, so only reading a certain amount of characters wont work.

Anyone have any idea how to do this? I really need to know this and I hope that this can help other people in the future.

thedogger
  • 33
  • 5
  • 5
    What part of the string do you need to read? You can do `str.split(':')` – Andrej Kesely Jun 09 '20 at 21:16
  • Does this answer your question? [Split a string by a delimiter in python](https://stackoverflow.com/questions/3475251/split-a-string-by-a-delimiter-in-python) – Giuseppe Jun 09 '20 at 21:20

1 Answers1

1

Getting the values into array as strings:

test_str = "1235:9875:0.1234"
number_str_arr = test_str.split(":")  # ['1235', '9875', '0.1234']

Saving them as floats instead of strings (maybe what you want?)

number_arr = [float(num) for num in number_str_arr]  # [1235.0, 9875.0, 0.1234]

How to access certain values:

first_num = number_arr[0]  # 1235.0
last_num = number_arr[-1]  # 0.1234
shmulvad
  • 636
  • 4
  • 15