Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
私は周りを見回しましたが、Pythonが英数字の文字列を数字の文字列に変換するための明確な答えがわかりません。これは、変換したい数値の例です。
"1234alpha" --> 1234 "a1234asdf" --> 0 "1234.56yt" --> 1234.56
アドバイスをいただければ幸いです。
DK
import re def str_to_int(string): match = re.match("\d+", string) if match: try: return int(match.group()) except ValueError: return float(match.group()) else: return 0 str_to_int("1234alpha") 1234 str_to_int("a1234asdf") 0