仅从 Python 中的字符串中提取字符

编程入门 行业动态 更新时间:2024-10-09 19:16:12
本文介绍了仅从 Python 中的字符串中提取字符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

在 Python 中,我只想从字符串中提取字符.

In Python, I want to extract only the characters from a string.

考虑我有以下字符串,

input = "{('players',): 24, ('year',): 28, ('money',): 19, ('ipod',): 36, ('case',): 23, ('mini',): 46}"

我想要的结果是,

output = "players year money ipod case mini"

我试图只考虑字母来拆分,

I tried to split considering only the alphabets,

word1 = st.split("[a-zA-Z]+")

但是分裂并没有发生.

推荐答案

你可以用 re 来做,但是字符串拆分方法不需要正则表达式,它需要一个字符串.

You could do it with re, but the string split method doesnt take a regex, it takes a string.

这是使用 re 的一种方法:

Heres one way to do it with re:

import re word1 = " ".join(re.findall("[a-zA-Z]+", st))

更多推荐

仅从 Python 中的字符串中提取字符

本文发布于:2023-11-30 12:34:11,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1649973.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:字符串   字符   Python

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!