1

Im trying to split

dimensions = "M 0 0 C 65 1 130 1 194.6875 0 C 195 17 195 33 194.6875 50 C 130 49 65 49 0 50 C 0 33 0 17 0 0 z"

into list form where if I wanted to get 194.6875 I could do

print(dimensions[8])

Im having trouble converting it to a list since there are multiple types.

3
  • You want the dimensions[8] to be a float or a string ? Commented Jun 20, 2020 at 15:26
  • float is what im going for Commented Jun 20, 2020 at 15:26
  • If this is an SVG path you're trying to parse, perhaps you want to have a look at pypi.org/project/svg.path Commented Jun 20, 2020 at 15:31

3 Answers 3

1

You can use the .split() method on a string and pass in the character you want to split on, i.e:

dimensions = "M 0 0 C 65 1 130 1 194.6875 0 C 195 17 195 33 194.6875 50 C 130 49 65 49 0 50 C 0 33 0 17 0 0 z"
listDimensions = dimensions.split(' ')
x = float(listDimensions[8])
print(x)

After that it is a case of changing the data type of the item you want with something like str(), int() or float()

Sign up to request clarification or add additional context in comments.

Comments

1
dimensions = "M 0 0 C 65 1 130 1 194.6875 0 C 195 17 195 33 194.6875 50 C 130 49 65 49 0 50 C 0 33 0 17 0 0 z"
dimensions = dimensions.split(" ")
print(float(dimensions[8]))

Comments

1

You could split on space and convert to float the values that are not alphabetic strings

dimensions = "M 0 0 C 65 1 130 1 194.6875 0 C 195 17 195 33 194.6875 50 C 130 49 65 49 0 50 C 0 33 0 17 0 0 z"
values = [val if val.isalpha() else float(val) for val in dimensions.split(" ")]

print(values) # ['M', 0.0, 0.0, 'C', 65.0, 1.0, 130.0, 1.0, 194.6875, 0.0, 'C', 195.0, 17.0, 195.0, 33.0, 194.6875, 50.0, 'C', 130.0, 49.0, 65.0, 49.0, 0.0, 50.0, 'C', 0.0, 33.0, 0.0, 17.0, 0.0, 0.0, 'z']
print(values[8], type(values[8])) # 194.6875 <class 'float'>

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.