简体   繁体   中英

How do I start reading from a certain character in a string?

I have a list of strings that look something like this:

"['id', 'thing: 1\nother: 2\n']"
"['notid', 'thing: 1\nother: 2\n']"

I would now like to read the value of 'other' out of each of them. I did this by counting the number at a certain position but since the position of such varies I wondererd if I could read from a certain character like a comma and say: read x_position character from comma. How would I do that?

Assuming that "other: " is always present in your strings, you can use it as a separator and split by it:

s = 'thing: 1\nother: 2'
_,number = s.split('other: ')
number
#'2'

(Use int(number) to convert the number-like string to an actual number.) If you are not sure if "other: " is present, enclose the above code in try-except statement.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM